18

我的标签:

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

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

4

3 回答 3

28

您做对了一切,只需关闭以下选项]之前的选项: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 )) %>
于 2013-03-06T16:50:28.213 回答
10

尝试这个:

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

这在 rails 4.2 中完美运行

于 2013-03-06T16:44:33.413 回答
1

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

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

5.在轨道上工作。

于 2017-12-18T21:08:04.423 回答