Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the f.select rails selected

My code:

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

The 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 am I getting this error?

like image 708
Wayne Chu Avatar asked Aug 07 '13 10:08

Wayne Chu


4 Answers

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 ; )

like image 131
Raindal Avatar answered Nov 02 '22 06:11

Raindal


You should pass :selected option to options_for_select method, like this:

<%= f.select :area, options_for_select([['a','a'],['b','b'],['c','c']], :selected => params[:area]), {}, { :class => 'span3 controls controls-row' } %>
like image 35
Marek Lipka Avatar answered Nov 02 '22 06:11

Marek Lipka


The above answers didn't work for me on Rails 6. This is what worked. Copied from one of the answers on Reddit

= f.select(:results, options_for_select(['Accepted', 'Not Accepted', 'Rejected', 'Acc', 'Rej', 'Information Only', 'N/A'], selected: @report.results || nil), { include_blank: "Select Result" }, { class: 'form-control select2', style: 'width: 100%;'})
like image 2
mayorsanmayor Avatar answered Nov 02 '22 06:11

mayorsanmayor


Below is what worked for me using f.select with custom names and values including a blank field while using the Ransack search. Thanks to mayorsanmayor's example for getting me started in the right direction. This also works for rails 5.1.7.

                <%= f.select(:status_eq, options_for_select([ ["Verified", "verified"], ["Non Verified","notVerified"]], selected: @q.status_eq || nil), {:include_blank => "Select Status"}, {class: 'form-control'}) %>
like image 1
beegal Avatar answered Nov 02 '22 06:11

beegal