Reputation: 333
I have this bit of code that produces a dropdown menu for me with a default value of Unassigned.
But, what I'd like to do is to have a placeholder, Select Location, on the dropdown menu, although by default the value selected is 'Unassigned'
Code:
= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), { placeholder: 'Select Location', include_blank: true }, class: 'form-control'
As you can see in the code, I'm setting a placeholder in the f.select { options }, but the dropdown menu still says "Unassigned"
Words of wisdom?
Upvotes: 3
Views: 2993
Reputation: 6531
1- Instead of using map to get selected option use find_by or where
<%=f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').first.id, {:prompt => '-- Select Location --'}),{class: 'form-control'}%>
Or
<%=f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.find_by(name: 'Unassigned').id, {:prompt => '-- Select Location --'}),{class: 'form-control'}%>
References: =
Upvotes: 0
Reputation: 557
Try this, this might work for you:
= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), data: { placeholder: 'Select Location' }, class: 'form-control'
Upvotes: 0
Reputation: 2541
try include_blank: 'Select Location'
= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), include_blank: 'Select Location' , class: 'form-control'
Upvotes: 0