Reputation: 259
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
Upvotes: 3
Views: 3399
Reputation: 1
<%= form.collection_select :msr_cd,
@msrs, :msr_cd, :msr_cd,
{multiple: false, required: true},
data: { placeholder: "Select Measure" },
class: "form-control col-sm-12 chosen-select"
%>
Note:
@msrs from the controller
:msr_cd - option value`enter code here`
:msr_cd - option text
We can pass the chosen select like above
Upvotes: 0
Reputation: 4320
Try this
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true}) %>
Explanation:
According to the Rails documentation the syntax for the collection_select function looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
As per the syntax options and html_options are hashes, so you need to enclose them in braces.
Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
Upvotes: 1
Reputation: 4649
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} %>
Upvotes: 10