我希望将以下collection_select
按State
分组,这是Tuning
上的一个字段,要么是Public
要么是Private
。这可能吗?
在视图中:
<%= collection_select :tunings, :tuning, @fretboard_tuning_options, :id, :name, {}, { :multiple => false, class: "", id: "tuning", style: 'width: 100%;' } %>
在控制器中:
@fretboard_tuning_options = Tuning.where('state=? OR user_id=?', 'Public', current_user.id)
任何具有公共状态的优化都不会与具有 user_id = current_user.id 的优化重叠。目标是有一个下拉框(我正在使用 Select2 来格式化下拉列表(,该下拉框显示两组选项:公共和私有,每组下是相关的调优。这可能吗?
谢谢!
你想要grouped_collection_select
:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select