我想添加此选项
:include_blank =>真
如何将其添加到此select_tag?
<%= select_tag :genre, options_for_select(Genre.all.map{ |g| [g.name, g.id] }) %>
答案 0 :(得分:2)
我在文档中找到了这个:
select_tag "people", options_from_collection_for_select(@people, "id", "name"), :prompt => "Select something"
所以你使用:prompt =&gt; “空白提示”
你的看起来可能是这样的:
select_tag :genre, options_from_collection_for_select(Genre.all.map{ |g| [g.name, g.id] }, "id", "name"), :prompt => "Select something"
此处的文档:http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html 看看select_tag
答案 1 :(得分:0)
您可以将选项include_blank
与select_tag
一起使用。
来自文档:
select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true
# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>
或者您可以使用options_for_select
:
<%= select_tag column.name, options_for_select(Genre.all.map{ |g| [g.name, g.id] }), :include_blank => true %>