Topic: Selected

I have a drop down menu and i want to initialize it with the value that is in the DB

<%= select_tag :state, options_for_select(us_states ,:state)  , {:class => 'input'} %>

but this line populates all 50 states but no matter what state is in the db it shows the first one

Re: Selected

As stated in Rails guides:

options_for_select allows you to pre-select an option by passing its value.
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>

output:

<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>
...

Whenever Rails sees that the internal value of an option being generated matches this value, it will add the selected attribute to that option.

The second argument to options_for_select must be exactly equal to the desired internal value. In particular if the value is the integer 2 you cannot pass “2” to options_for_select — you must pass 2. Be aware of values extracted from the params hash as they are all strings.