rails, simple_form, how to set selected index of a collection when page loaded?

simo picture simo · Feb 2, 2012 · Viewed 53.8k times · Source

I am using simple_form gem, I have a countries collection, it work fine when I select the country, and updated record will have the country id stored, but, when I try to edit the record, the chosen country is not selected by default at edit form.

Here is the code at edit form:

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

Shouldn't simple_form view the selected country from the db ?

Answer

Manish Shrivastava picture Manish Shrivastava · Feb 2, 2012

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

:selected => selected_country_id

So,

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

This will work perfectly !!!

Cheers!