22

这是我的代码:

<%= 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

4 回答 4

37

无需使用: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]将被选中。

希望能帮助到你 ; )

于 2013-08-07T14:19:34.310 回答
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' } %>
于 2013-08-07T10:32:43.587 回答
2

以上答案在 Rails 6 上对我不起作用。这就是有效的。复制自 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%;'})
于 2020-01-05T09:47:06.810 回答
1

以下是在使用 Ransack 搜索时使用 f.select 和自定义名称和值(包括空白字段)对我有用的方法。感谢市长的榜样让我朝着正确的方向开始。这也适用于 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'}) %>
于 2020-07-06T15:32:36.187 回答