Grouped Select in Rails

后端 未结 2 1476
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 06:06

Simple question really - how do I use the select(ActionView::Helpers::FormOptionsHelper) with grouped options?

I have got it working with a select_tag (ActionView::H

2条回答
  •  猫巷女王i
    2020-12-09 06:36

    You can also use a Hash instead of nested arrays:

    grouped_options = {
     'North America' => [['United States','US'], 'Canada'],
     'Europe' => ['Denmark','Germany','France']
    }
    
    
    <%= select_tag "foo[bar]", 
    grouped_options_for_select(grouped_options, 'Denmark') %>
    

    Here, there is also a selected option ('Denmark')

提交回复
热议问题