Reputation: 1771
Is it possible to do a grouped_collection_select on a single model? Most examples I've seen use two models.For example this Railscast: http://railscasts.com/episodes/88-dynamic-select-menus-revised
I have a Venue model with fields for venue name and town (amongst other things). Currently my venue select is huge and i'd like to group by town. Do I need to move towns into a separate model?
Upvotes: 2
Views: 1069
Reputation: 3540
Where do you store the towns now? It could be a better solution anyway to have towns in another model, but that depends on other things, like if you thing the towns data will change over time. However you can easily add a grouped select without a new model. Try something like this:
<% form_for @venue do |f| %>
<%= f.select :town, grouped_options_for_select(@towns) %>
...
However the @towns
should be and 2-dimensional array like this
[
["Group1",
["Town1", "Town2", "Town3"]
],
["Group2",
["Town4", "Town5", "Town6"]
]
]
You can see more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_options_for_select
Otherwise if you have an example of some data for towns and your current form, could i help you more :)
Upvotes: 1