我试图通过选择用户列表中的一个(用户has_many列表和列表属于用户)来向select标签添加可选选项。 这是我的控制器代码(假设我已经定义了@user):
@options_for_select = []
@user.lists.each do |list|
@options_for_select.push([list.title, list.id])
end
然后,在我看来,我有这个:
<%= select_tag(:option, @options_for_select, selected: :option, class: "form-control" ) %>
问题是,当我puts "options for select are " + @options_for_select.inspect
时,我得到了这个:
options for select are [["List 1", 1], ["List 2", 2], ["List 3", 3]
那为什么它不起作用?
答案 0 :(得分:0)
我所要做的就是在我的代码中包含options_for_select(@options_for_select)。而不是
<%= select_tag(:option, @options_for_select, selected: :option, class: "form-control" ) %>
我做了
<%= select_tag(:option, options_for_select(@options_for_select), selected: :option, class: "form-control" ) %>
哎。
答案 1 :(得分:0)
您可以传递:include_blank => false
选项:
= f.input :option, :as => :select, :collection => options_for_select(@options_for_select), :include_blank => false