ruby-on-railsformsruby-on-rails-4fieldform-for

Required Field on collection_select type field


I have a form with a simple text field where I require the form to be filled out:

<%= f.text_field :email, :required => true %>

The next field is a collection_select type where I want to force the user to select a choice. I tried:

<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %>

which gives me the error:

syntax error, unexpected ',', expecting ')' ..., :name, {}, {multiple: true}), :required => true );@output_... ... ^

Without the :required => true option the code works fine. How do I force a selection by the user in this case? Thanks


Solution

  • Try changing this

    <%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %>
    

    to this

    <%= f.collection_select :list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true} %>