Rails选择标签选择的值

时间:2013-03-06 16:33:21

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我的标签:

<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5], :selected => :option ])) %>

如何将所选值设置为选择了哪个选项。例如,如果我选择['Bought', 3]并提交,则会选择['All', 1]选项。如何在提交表单后显示所选值。

3 个答案:

答案 0 :(得分:26)

您已完成所有操作,只需在]

之前关闭选项:selected => :option即可

而不是...], selected: :option ])),请更改为...]], selected: :option ))

所以,你的代码应该是:

<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]], selected: :option )) %>

答案 1 :(得分:10)

试试这个:

<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]], :selected => params[:option])) %>

这在rails 4.2中非常有效。

答案 2 :(得分:1)

如果您想在标签中添加一个类:

<%= select_tag(:option, options_for_select([["Option 1",1],["Option 2",2],["Option 3",3]], params[:option] ), class:"select") %>

在铁轨5中担心。