Select options with option groups
ActionView::Helpers::FormOptionsHelper#grouped_options_for_select lets you pass in a nested array of strings, and returns a string of option
tags wrapped with optgroup
tags:
The first value serves as the optgroup
label while the second value must be an array of options.
grouped_options = [
["North America",
["United States", "Canada"]],
["Europe",
["Denmark", "Germany", "France"]]
]
Next, simply call it from a form helper in the view, passing in your grouped options.
<%= f.select :location, grouped_options_for_select(grouped_options) %>
Voila! You should now have something like this: