B S*_*ven 60 html-select ruby-on-rails erb ruby-on-rails-3
我试过了:include_blank => true,但没办法.
<select>
<%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>
Run Code Online (Sandbox Code Playgroud)
如果我需要将它添加到集合中,你会怎么做?
Chr*_*tto 86
我想你想要这种格式:
select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})
Run Code Online (Sandbox Code Playgroud)
BTW:假设Modle被认为是Model.要使用collection_select:
collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)
Run Code Online (Sandbox Code Playgroud)
Dyl*_*kow 26
我相信这些:include_blank选项仅适用select于与模型相关的字段.
假设您要使用普通<select>标签而不是<%= select(...) %>绑定到模型,您可以在结果的前面插入一个空白条目:
<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>
Run Code Online (Sandbox Code Playgroud)
Pau*_*lgo 19
既然你已经标记为选择标签,你可以使用的选项include_blank有select_tag.
从文档:
select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true
# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>
Run Code Online (Sandbox Code Playgroud)
或者您可以使用options_for_select:
<%= select_tag column.name, options_for_select(Model.all.collect{|mt| [mt.name, mt.id]}), :include_blank => true %>
Run Code Online (Sandbox Code Playgroud)
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71408 次 |
| 最近记录: |