How to make the f.select rails selected

Wayne Chu picture Wayne Chu · Aug 7, 2013 · Viewed 32.3k times · Source

It's my code:

<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']]), {}, {:class => 'span3 controls controls-row'}, :selected => params[:area] %>

and result is:

ArgumentError in Users#edit
Showing /home/airson/rails_projects/friends_of_local/app/views/users/edit.html.erb where line #17 raised:
wrong number of arguments (5 for 4)

why.....@@?

Answer

Raindal picture Raindal · Aug 7, 2013

No need to use :selected just pass your params[:area] alone to options_for_select as a second argument:

<%= f.select :area, 
    options_for_select([['a','a'],['b','b'],['c','c']], params[:area]),
    {}, { :class => 'span3 controls controls-row' } %>

The last value of your params[:area] will be selected.

Hope it helps ; )