Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在选择标记的选项中,为选择列表中的第一项包含空白_Ruby On Rails_Ruby On Rails 3_Erb_Html Select - Fatal编程技术网

Ruby on rails 在选择标记的选项中,为选择列表中的第一项包含空白

Ruby on rails 在选择标记的选项中,为选择列表中的第一项包含空白,ruby-on-rails,ruby-on-rails-3,erb,html-select,Ruby On Rails,Ruby On Rails 3,Erb,Html Select,我尝试了:include_blank=>true,但没有成功 <select> <%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %> </select> 如果我需要将其添加到收藏中,您将如何操作?我想您需要这种格式: select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ]

我尝试了
:include_blank=>true
,但没有成功

<select>
    <%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>


如果我需要将其添加到收藏中,您将如何操作?

我想您需要这种格式:

select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})
顺便说一句:假设模型是模型。要使用集合,请选择:

collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)

我相信
:include_blank
选项仅适用于绑定到模型的
select
字段

假设您希望使用普通的
标记,而不是绑定到模型的
,您可以在结果的前面插入一个空白条目:

<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>

既然您已标记为选择标签,您可以使用带有
选择标签的选项
包含空白

从文件中:

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>

如果你想要一个灵巧的解决方案,你可以使用my gem,它有一个功能,可以安全地为_select
和_collection _select修补
选项

rails g重新装备的导轨:设置

打开
config/initializers/realmed_rails.rb
并将以下值更改为
true

options_for_select_include_blank: true,
options_from_collection_for_select_include_blank: true

现在,只要您需要包含空白内容,只需执行以下操作:

<%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>


我相信rails 2和rails 3也是这样?您可能希望使用集合\u select helper而不是html select标记-有关include\u blank的更多信息-它也适用于许多其他Rails帮助程序。我刚刚尝试了
grouped\u collection\u select
,效果很好。谢谢@chrisAnytime@htaidirt。很高兴能帮助你,如果你使用了<代码>选择TytAtg/<代码>,并且你想为空白选项包含一些文本,你需要使用<代码>提示<代码>,因为<代码>包含空白> <代码>仅为真/假。“:include_blank-如果select元素的第一个选项元素为空,则设置为true或提示字符串。如果select元素不需要默认值,则此选项非常有用。“这在Rails 4.2和更早版本中都适用。@Maltriel在Rails 3上使用
select_tag
,如果传递字符串,则不会显示该字符串。我期待它像其他select方法一样,但它没有。这就是为什么我留下评论来帮助别人。您可以在这里看到源代码,只需删除
include_blank
值:这是问题的实际答案。其他涉及更改输入结构。仅供参考,使用
选择标记
(请注意
\u标记
部分)
包含空白
不会显示传递给它的字符串。您需要使用
prompt
来代替该选项。这显示了一个空白选项,但存在一个问题:当编辑具有字段指定值的现有实体时,不会加载数据库中存储的值,但显示的始终是一个空白选项。这是错误的
include\u blank:
是select\u tag方法上的一个选项,因此不管怎样它都会起作用。@tubbo
include\u blank:
很棒,是rails的最佳方式选项。但当只更新集合参数时,这段棘手的代码非常有用。
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>
options_for_select_include_blank: true,
options_from_collection_for_select_include_blank: true
<%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>