空白 select 标签形式
Blank select tag form
我正在尝试通过 select 用户列表之一(用户 has_many 列表和一个列表属于一个用户)。
这是我的控制器代码(假设我已经定义了@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" ) %>
但这会呈现一个没有 select 选项的空白 select 标签。
事情是,当我做 puts "options for select are " + @options_for_select.inspect
时,我得到这个:
options for select are [["List 1", 1], ["List 2", 2], ["List 3", 3]
为什么它不起作用?
我所要做的就是在我的代码中包含 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" ) %>
哎呀
您可以传递一个 :include_blank => false
选项:
= f.input :option, :as => :select, :collection => options_for_select(@options_for_select), :include_blank => false
我正在尝试通过 select 用户列表之一(用户 has_many 列表和一个列表属于一个用户)。 这是我的控制器代码(假设我已经定义了@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" ) %>
但这会呈现一个没有 select 选项的空白 select 标签。
事情是,当我做 puts "options for select are " + @options_for_select.inspect
时,我得到这个:
options for select are [["List 1", 1], ["List 2", 2], ["List 3", 3]
为什么它不起作用?
我所要做的就是在我的代码中包含 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" ) %>
哎呀
您可以传递一个 :include_blank => false
选项:
= f.input :option, :as => :select, :collection => options_for_select(@options_for_select), :include_blank => false