Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails form_for select tag with option selected

I am using a form_for to update a user profile.

As part of the form I am using a select menu. The menu is filled from an array ie,

STATUS = [['Active', 'active'], ['In Active', 'inactive']]

Then in the form

<%= f.select(:status, options_for_select(STATUS)) %>

While this work's its not selecting the option which was previously selected on edit.

Am I missing something ?

like image 343
Lee Avatar asked Nov 30 '11 19:11

Lee


2 Answers

<%= f.select(:status, options_for_select(STATUS, :selected => params[:status])) %>

should be written as

<%= f.select :status, STATUS %>
like image 84
fl00r Avatar answered Nov 09 '22 05:11

fl00r


In the case that params doesn't do it for you, try

<%= f.select(:status, options_for_select(STATUS, :selected => f.object.status)) %>
like image 29
stevenspiel Avatar answered Nov 09 '22 05:11

stevenspiel