kdubss
kdubss

Reputation: 333

Rails f.select with a default value, but a different place holder

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

Answers (3)

Anand
Anand

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: =

http://api.rubyonrails.org/v5.2.0/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_options_for_select

Upvotes: 0

Rasna Shakya
Rasna Shakya

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

Shani
Shani

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

Related Questions