How to make the f.select rails selected

眉间皱痕 提交于 2019-12-03 15:20:20

问题


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.....@@?


回答1:


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




回答2:


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' } %>


来源:https://stackoverflow.com/questions/18100834/how-to-make-the-f-select-rails-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!