Rails, simple_form, how to set the selected collection index when the page loads?

I use the simple_form pearl, I have a collection of countries, it works fine when I select a country, and the updated record will have a country identifier, but when I try to edit the record, the selected country is not selected by default in the editing form.

Here is the code in the edit form:

= f.input :country_id, :collection => all_countries 

Should simple_form view the selected country from db?

+49
ruby-on-rails ruby-on-rails-3 simple-form
02 Feb 2018-12-12T00:
source share
2 answers

Have you tried to use the option: selected =>?

 :selected => selected_country_id 

So,

 = f.input :country_id, :collection => all_countries, :selected => selected_country_id 

This will work just fine !!!

Hurrah!

+115
02 Feb '12 at 7:11
source share

I know this was answered, but I came here to find a similar solution for a collection of flags. For posterity, here's how you do it:

 <%= f.input :country_ids, :as => :check_boxes, :collection => [['USA', :USA], ['Japan', :JPN]], :checked => [:JPN], :include_hidden => false %> 

Hope this helps someone.

+7
Jan 29 '14 at 1:19
source share



All Articles