Ruby-on-rails 在 options_for_select 标签的选择列表中包含第一项的空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6710965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include blank for first item in select list in options_for_select tag
提问by B Seven
I tried :include_blank => true, but it didn't work.
我试过了:include_blank => true,但没有用。
<select>
<%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>
If I need to add it to the collection, how would you do that?
如果我需要将它添加到集合中,你会怎么做?
回答by Chris Barretto
I think you want this format:
我想你想要这种格式:
select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})
BTW: was assuming Modle was suppose to be Model. To use using collection_select:
顺便说一句:假设模型应该是模型。要使用 collection_select 使用:
collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)
回答by Dylan Markow
I believe the :include_blankoptions only exist for selectfields tied to a model.
我相信这些:include_blank选项仅适用select于与模型相关的字段。
Assuming you want to use a plain <select>tag instead of a <%= select(...) %>tied to a model, you can insert a blank entry at the front of your results:
假设您想使用普通<select>标签而不是<%= select(...) %>绑定到模型,您可以在结果前面插入一个空白条目:
<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>
回答by Paulo Fidalgo
Since you have tagged as select-tag you can use the option include_blankwith select_tag.
由于您已标记为 select-tag,因此您可以将该选项include_blank与select_tag.
From the documentation:
从文档:
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>
Or you can use options_for_select:
或者你可以使用options_for_select:
<%= select_tag column.name, options_for_select(Model.all.collect{|mt| [mt.name, mt.id]}), :include_blank => true %>
回答by Weston Ganger
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>
回答by Weston Ganger
If you want a slick solution you can use my gem rearmed_railswhich has a feature in it that safely monkey patches options_for_selectand options_for_collection_select
如果你想要一个巧妙的解决方案,你可以使用我的 gem rearmed_rails,它有一个安全的猴子补丁options_for_select和options_for_collection_select
rails g rearmed_rails:setup
rails g rearmed_rails:setup
Open config/initializers/rearmed_rails.rband change the following values to true
打开config/initializers/rearmed_rails.rb并将以下值更改为true
options_for_select_include_blank: true,
options_from_collection_for_select_include_blank: true
Now whenever you need a blank included simply do the following:
现在,每当您需要包含空白时,只需执行以下操作:
<%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>
回答by Simon Liu
= select_tag "some_value", options_for_select(Model.all.collect{ |x| [x.name, x.id]}.prepend([t('helpers.some_name'), nil]), :selected => params[:some_value])

