如何在options_from_collection_for_select中添加"select one ..."

BBQ*_*hef 24 ruby-on-rails

以下是我的选择表格,它正常工作.

当用户加载页面时,它应显示初始的"选择一个......",其值为null或''.

我试图将它添加到Object但是无法并且很乐意获得帮助!

非常感谢!


在我看来:

= select_tag 'incident[fault_id]' , options_from_collection_for_select( Fault.all, :id, :label)
Run Code Online (Sandbox Code Playgroud)

我使用Rails 3.2和HAML


更新:

我偶然发现了一些非常甜蜜的东西:

include_blank: 'select one...'
Run Code Online (Sandbox Code Playgroud)

或完全

= f.collection_select :fault_id, Fault.order(:label), :id, :label, include_blank: 'select one...'
Run Code Online (Sandbox Code Playgroud)

如果有人也喜欢......

参考:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

rog*_*111 43

options_from_collection_for_select返回一个选项标记字符串,这些选项标记是通过迭代集合并将调用结果分配给value_method作为选项值并将text_method作为选项文本分配而编译的.

所以只需在前面加上"select_one"选项字符串,不带值:

 = select_tag 'incident[fault_id]', content_tag(:option,'select one...',:value=>"")+options_from_collection_for_select( Fault.all, :id, :label)
Run Code Online (Sandbox Code Playgroud)


dab*_*ert 25

:prompt是的属性select_tag options_from_collect_for_select那么

select_tag("sales_rep[manufacturer_id]", options_from_collection_for_select(@manufacturers, "id", "name"), { :prompt => 'Select Manufacturer' })
Run Code Online (Sandbox Code Playgroud)