如何选择f.select导轨

时间:2013-08-07 10:20:52

标签: ruby-on-rails select option selected

这是我的代码:

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

结果是:

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)

为什么..... @@?

4 个答案:

答案 0 :(得分:31)

无需使用:selected仅将params[:area]单独传递给options_for_select作为第二个参数:

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

将选择params[:area]的最后一个值。

希望它有所帮助; )

答案 1 :(得分:14)

您应该将:selected选项传递给options_for_select方法,如下所示:

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

答案 2 :(得分:1)

下面是使用R.ack搜索时使用带有自定义名称和值(包括空白字段)的f.select对我有用的方法。多亏了mayorsanmayor的榜样,使我朝着正确的方向开始。这也适用于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'}) %>

答案 3 :(得分:0)

以上答案在Rails 6上对我不起作用。 这是有效的。 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%;'})