Ruby Rails 集合选择显示空白的“提示”值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/760566/
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
Ruby Rails collection select is displaying blank "prompt" value?
提问by Tony
I have a collection select like the following:
我有一个像下面这样的集合选择:
<%= f.collection_select :region_id, Region.find(:all), :id, :name, { :prompt => 'Select a State/Province' }, :style => "width: 200px;" %>
Sometimes the prompt from the :prompt option appears, but sometimes it does not. Does anyone know where I could begin to troubleshoot this? Maybe I have been looking at it too long...
有时会出现 :prompt 选项的提示,但有时不会。有谁知道我可以从哪里开始解决这个问题?可能是我看的太久了...
回答by
:include_blankwith the value of your blank option seems to do the trick. Try this:
:include_blank使用空白选项的值似乎可以解决问题。尝试这个:
{:include_blank => "Please select"}
回答by robotdana
:promptappears in the list when there isn't a selected value.
:prompt当没有选定的值时,会出现在列表中。
:include_blankappears in the list always, even if you've loaded the select with a selected value.
:include_blank始终出现在列表中,即使您加载了带有选定值的选择。
if you want your select to always have "Select a State/Province"as the first option:
如果您希望您的选择始终"Select a State/Province"作为第一个选项:
<%= f.collection_select :region_id, Region.all, :id, :name, include_blank: 'Select a State/Province' %>
if you want your collection to have "Select a State/Province"as the first option only when a region is not already selected:
如果您希望您的收藏"Select a State/Province"仅在尚未选择区域时作为第一个选项:
<%= f.collection_select :region_id, Region.all, :id, :name, prompt: 'Select a State/Province' %>
source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
来源:http: //api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
回答by Robin Taylor
I've been having the same problem. Using 'prompt' seems to create an attribute for the select tag, problem is there is no such attribute that I know of. Plus its clearly not what is described in the Rails docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select.
我一直有同样的问题。使用“提示”似乎为选择标签创建了一个属性,问题是我不知道这样的属性。加上它显然不是 Rails 文档中描述的内容http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select。
Using include_blank seems a good workaround for me.
使用 include_blank 对我来说似乎是一个很好的解决方法。
Cheers, Robin.
干杯,罗宾。
回答by Sudhir Vishwakarma
try this <%= f.collection_select :region_id, Region.all, :id, :name, {prompt: 'Select a State/Province'}, {class: "form-control"} %>
尝试这个 <%= f.collection_select :region_id, Region.all, :id, :name, {prompt: 'Select a State/Province'}, {class: "form-control"} %>
回答by nitecoder
Instead of
代替
:prompt => "Select a State/Province"
try
尝试
:allow_blank => "Select a State/Province"
EDIT: Yes after checking the API I can see that I had it confused, prompt is the correct way according to the documentation, could it be that it only sometimes it appears because your object has a value already and therefore the prompt is there but it is not the currently selected value in the drop down list???
编辑:是的,在检查 API 后,我可以看到我把它弄糊涂了,根据文档提示是正确的方法,可能只是有时它出现是因为您的对象已经有一个值,因此提示在那里但是它不是下拉列表中当前选择的值???

