Reputation: 2630
I would like to have the following collection_select
be grouped by State
, which is a field on Tuning
that is either Public
or Private
. Is this possible?
In the view:
<%= collection_select :tunings, :tuning, @fretboard_tuning_options, :id, :name, {}, { :multiple => false, class: "", id: "tuning", style: 'width: 100%;' } %>
In the controller:
@fretboard_tuning_options = Tuning.where('state=? OR user_id=?', 'Public', current_user.id)
Any tuning with a State of Public will not overlap with those that have user_id = current_user.id. The goal is to have a dropdown box (I am using Select2 to format the dropdown) that shows two groups of options: Public and Private, and under each group are the relevant Tunings. Is this possible?
Thanks!
Upvotes: 1
Views: 821
Reputation: 4966
You want grouped_collection_select
: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select
Upvotes: 2