如何添加':include_blank =>真的'到自定义select_tag?

时间:2013-01-04 20:43:36

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

我想添加此选项

  

:include_blank =>真

如何将其添加到此select_tag?

 <%= select_tag :genre, options_for_select(Genre.all.map{ |g| [g.name, g.id] }) %>

2 个答案:

答案 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_blankselect_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 %>