我有一个数组@name = ["D2", "D3"]
,因此如何在select_tag
中将其设置为选定的选项。
我正在尝试这个,但没有显示
<%= select_tag "add", options_for_select(["D1","D2","D3","D4"],{
@name.each do |id| :selected =>"#{id}" } ), {:multiple => true}
答案 0 :(得分:2)
那是怎么回事:
<%= select_tag "add", options_for_select(["D1","D2","D3","D4"],@name), {:multiple => true} %>
答案 1 :(得分:1)
你需要这样的东西:
<%= select_tag "add", options_for_select(["D1","D2","D3","D4"], :selected =>["D1","D4"]) , :multiple => true %>
除非您name.id
给出“D1”,否则您可能想要改变您的方法。 name
中的哪个字段对应“D1”?