Ruby-on-rails 如何命名选择中的空白值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10036050/
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
How to name the blank value in select?
提问by Ben
I use simple_form in my app.
我在我的应用程序中使用 simple_form。
How do i give the blank value in my selects a different text than "" ?
我如何在我的选择中给出与 "" 不同的文本的空白值?
I just find an option to include blank or not.
我只是找到一个选项是否包含空白。
回答by Josh Moore
Ben,
本,
It depends on how you are constructing your options for select. If you're doing it like the code below, just pass a string into the :include blank.
这取决于您如何构建选择选项。如果你像下面的代码那样做,只需将一个字符串传递到 :include 空格中。
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Some text here'})
If you're setting the options with a options_for_select(), then you can do something like the following:
如果您使用 options_for_select() 设置选项,那么您可以执行以下操作:
options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
With the value="" being the second value in the array and the name that shows up in the dropdown being first. So in your case, you could change the second answer to look like this:
value="" 是数组中的第二个值,下拉列表中显示的名称是第一个。因此,在您的情况下,您可以将第二个答案更改为如下所示:
options_for_select([["Some text here", ""], ["Dollar", "$"], ["Kroner", "DKK"]])
回答by Demetris Constantinou
Instead of
代替
:include_blank => true
Try
尝试
:include_blank => "your text here"
if this is what you are looking.
如果这就是你正在寻找的。
回答by Georgi
If you are using the select_tag(name, option_tags = nil, options = {})function, the correct option is :prompt => "Some text"rather than setting a string value for select
如果您正在使用该select_tag(name, option_tags = nil, options = {})函数,正确的选项是:prompt => "Some text"而不是为select
回答by tsherif
You can do this manually by adding ["Your Text", ""]to the beginning of the array passed to options_for_select, or add "<option value=\"\">#{h("Your Text"}</option>"to beginning of the string passed to select_tag.
您可以通过添加["Your Text", ""]到传递给 的数组的开头options_for_select或添加"<option value=\"\">#{h("Your Text"}</option>"到传递给 的字符串的开头来手动执行此操作select_tag。

