Ruby-on-rails 我可以在 select_tag 中包含空白字段吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1943106/
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
Can I include blank field in select_tag?
提问by user235195
Is it possible to add an option like :include_blank => 'Please Select'in a method select_taglike it is possible with the selectmethod? It seems not to work. Is there any substitute for this for select_tagmethod?
是否可以像:include_blank => 'Please Select'在方法中 select_tag一样添加一个选项 select?它似乎不起作用。这个select_tag方法有什么替代方法吗?
回答by maro
In Rails 3 there is a :promptoption for select_tag:
在 Rails 3 中有一个:prompt选项select_tag:
select_tag "things", many_thing_as_options, :prompt => "Please select"
回答by EmFi
The select_tag method does not modify the options list you pass it. If you want a blank option you have to include it in your list of options.
select_tag 方法不会修改您传递给它的选项列表。如果您想要一个空白选项,则必须将其包含在您的选项列表中。
If you're using options_for_select your list should start with the blankitem, ie: ["Please select", ''].
如果您使用 options_for_select,您的列表应以空白项开头 ,即:["Please select", '']。
If you're just passing html to select_tag make sure your first option is:
如果您只是将 html 传递给 select_tag,请确保您的第一个选项是:
<option value="">Please Select</option>
回答by schreifels
Note that in Ruby on Rails 3, select_tag()will accept an :include_blankboolean argument (same with date_select and the like).
请注意,在 Ruby on Rails 3 中,select_tag()将接受一个:include_blank布尔参数(与 date_select 等相同)。

