使用select_tag显示多个选定的值?

时间:2013-05-16 11:37:24

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

我有一个数组@name = ["D2", "D3"],因此如何在select_tag中将其设置为选定的选项。

我正在尝试这个,但没有显示

<%= select_tag "add", options_for_select(["D1","D2","D3","D4"],{
@name.each do |id| :selected =>"#{id}" } ), {:multiple => true}

2 个答案:

答案 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”?