在select_tag中提示
在我的用户注册申请中,我有一个国家选择器..
我想添加一个提示作为第一个默认值(类似“— select country —”)。 我应该在哪里以及如何选择此选项?
使用FormHelper :prompt
select(:user, :country, options_for_select(@COUNTRIES), {:prompt => "--select county--"})
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper
您也可以像这样给出自定义的提示值
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"select User name")
非常简单:
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
对于提示“请选择”,或者这是您的自定义文本:
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"Select country")
还要注意@COUNTRIES
是错误的,实例变量应该是小写的 – @countries
,一个@countries
就是COUNTRIES
。
如果有人提到这个,请尝试在options_for_select之外写prompt
。
代替
select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)
尝试,
select(:user, :country, options_for_select(@COUNTRIES)),{:prompt=>"Your message here"}
也适用于select_tag。 此外,我同意@gunn的命名惯例。
collection_select(:product, :category_id, Category.all, :id, :title, {:prompt => true} ) collection_select(:product, :category_id, Category.all, :id, :title, {:include_blank => 'Please Select'} )
这两个都导致相同的html,但当您返回编辑以前创建的产品时,第一个将不包括’请选择’选项