kdubss
kdubss

Reputation: 333

Drop down menu with collection_select: Dividing the dropdown into sections

I have a dropdown menu for locations, which pulls location data from the DB.

@locations

...is an ActiveRecord::Associations::CollectionProxy object, containing various locations (i.e. loc1, loc2, locY)

What I would really like to do is to have the dropdown divide the locations based on categories (e.g. Numbered Locations: loc1, loc2; Lettered Locations: locY, etc.).

The code thus far is (in a slim file),

f.collection_select :location_id, current_provider.locations.except_unassigned, :to_s, { include_blank: true }, class: 'form-control'

Does anyone have suggestions on how to start?

Many thanks!

Upvotes: 0

Views: 149

Answers (1)

Anitha Narayanasamy
Anitha Narayanasamy

Reputation: 21

You can use grouped_options_for_select and achieve this.

<% locations = {'Numbered Locations' => ['loc1', 'loc2'],'Lettered Locations' => ['locX', 'locY'] } %>

<%= select_tag :city, grouped_options_for_select(locations) %>

Upvotes: 0

Related Questions