How to create a grouped select box using simple_form?

Swartz picture Swartz · Oct 27, 2010 · Viewed 16.6k times · Source

I'm using simple_form gem for creating Rails forms. http://github.com/plataformatec/simple_form

All is great, except how do I create a grouped select box? Can't find it in the docs or by google-ing.

Answer

dolzenko picture dolzenko · Jun 14, 2012

The question is old but it's the top result for "simple_form grouped select" google search anyway so I figured the next reader might benefit from a few creative ways to create these with the latest simple_form (taken from tests, which are always the best documentation indeed)

<%= f.input :author,
 :as => :grouped_select,
 :collection => [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]],
 :group_method => :last %>

<%= f.input :author,
 :as => :grouped_select,
 :collection => Proc.new { [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]] },
 :group_method => :last %>

<%= f.input :author,
 :as => :grouped_select,
 :collection => { ['Jose', 'Carlos'] => 'Authors' },
 :group_method => :first,
 :group_label_method => :last %>

<%= f.input :author,
 :as => :grouped_select,
 :collection => { 'Authors' => ['Jose', 'Carlos'] },
 :group_method => :last,
 :label_method => :upcase,
 :value_method => :downcase %>