Rails f.select具有默认值,但占位符不同

时间:2018-06-25 22:30:55

标签: ruby-on-rails ruby ruby-on-rails-4 form-helpers

我有这段代码可以为我生成一个下拉菜单,默认值为Unassigned。

但是,我想做的是在下拉菜单上有一个占位符选择位置,尽管默认情况下所选的值为“未分配”

代码:

= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), { placeholder: 'Select Location', include_blank: true }, class: 'form-control'

您可以在代码中看到,我在 f.select {options}中设置了一个占位符,但是下拉菜单中仍然显示“未分配”

智慧的话?

3 个答案:

答案 0 :(得分:0)

尝试include_blank: 'Select Location'

= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), include_blank: 'Select Location' , class: 'form-control'

答案 1 :(得分:0)

尝试一下,这可能对您有用:

= f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').map { |loc| loc.id }), data: { placeholder: 'Select Location' }, class: 'form-control'

答案 2 :(得分:0)

1-使用find_by或where

而不是使用地图来获取选择的选项
 <%=f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.where(name: 'Unassigned').first.id, {:prompt => '-- Select Location --'}),{class: 'form-control'}%>

 <%=f.select :training_location_id, grouped_options_for_select(grouped_location_options, current_provider.locations.find_by(name: 'Unassigned').id, {:prompt => '-- Select Location --'}),{class: 'form-control'}%>

参考:=

http://api.rubyonrails.org/v5.2.0/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_options_for_select