Ruby-on-rails select_tag 中的提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4668658/
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
Prompt in select_tag
提问by Kreeki
In my application in user registration I have a country picker..
在我的用户注册应用程序中,我有一个国家/地区选择器..
<%= select(:user, :country, options_for_select(@COUNTRIES)) %>
And I want to add a prompt as a first default value (something like "--- select country ---"). Where and how should I put this option?
我想添加一个提示作为第一个默认值(类似于“---选择国家---”)。我应该在哪里以及如何放置这个选项?
回答by Jed Schneider
Use the FormHelper :prompt
使用表单助手 :prompt
select(:user, :country, options_for_select(@COUNTRIES), {:prompt => "--select county--"})
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper
回答by user1875926
You can also give customized prompt value like this
您还可以像这样提供自定义提示值
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"select User name")
回答by gunn
Very simple:
很简单:
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
For the prompt "Please select", or this for your custom text:
对于提示“请选择”,或者对于您的自定义文本:
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"Select country")
Also note that @COUNTRIESis wrong, an instance variable should be lowercase - @countries, a contant would just be COUNTRIES.
另请注意,这@COUNTRIES是错误的,实例变量应该是小写的 - @countries,而常量应该是COUNTRIES.
回答by longpc
collection_select(:product,
:category_id,
Category.all,
:id,
:title,
{:prompt => true}
)
collection_select(:product,
:category_id,
Category.all,
:id,
:title,
{:include_blank => 'Please Select'}
)
both of these result in the same html, but the first one will not include the 'Please Select' option when you return to edit the previously created Product
这两个结果都在同一个 html 中,但是当您返回编辑之前创建的产品时,第一个将不包含“请选择”选项
回答by Kaushik Thirthappa
In case if anyone comes referring to this, try writing the promptoutside options_for_select.
如果有人提到这个,请尝试编写prompt外部 options_for_select。
instead of
代替
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
try,
尝试,
select(:user, :country, options_for_select(@COUNTRIES)),{:prompt=>"Your message here"}
select(:user, :country, options_for_select(@COUNTRIES)),{:prompt=>"Your message here"}
Works perfectly for select_tag as well. Also, i agree with @gunn with the naming convention.
也适用于 select_tag。另外,我同意@gunn 的命名约定。

