Rails: helper form helper to run by default

I have a simple form. Also, as part of the form, I have a variable with an array / list of all country names. The form picks up the list just fine, however it starts with the first value, I think its Afghanistan , however I want it to start / default with United Kingdom

This is my code to select a form for countries.

 <%= f.select(:country, @country_list.map { |value| [ value, value ] }) %> 

I tried without success:

 <%= f.select(:country, @country_list.map { |value| [ value, value ] },['United Kingdom']) %> 
+4
source share
2 answers

Have you tried the selected option

 :selected => "United Kingdom" or your_country_id 

not sure about syntax but hopefully it will work

+3
source

From the documentation :

Set: selected => to use another choice or: selected => nil to leave all options unselected.

So, if you want the default to be selected, try the following:

 f.select(:country, @country_list.map { |value| [ value, value ] }, selected: "United Kingdom") 
+2
source

All Articles