如何使用simple_form创建分组选择框? [英] How to create a grouped select box using simple_form?
问题描述
我使用simple_form gem来创建Rails窗体。
http://github.com/plataformatec/simple_form
I'm using simple_form gem for creating Rails forms. http://github.com/plataformatec/simple_form
一切都很好,除了我如何创建分组选择框?无法在文档或Google搜索中找到它。
All is great, except how do I create a grouped select box? Can't find it in the docs or by google-ing.
推荐答案
这个问题很古老,但它是 simple_form分组选择谷歌搜索无论如何,所以我想下一个读者可能会受益于一些创造性的方式来创建这些与最新的simple_form(从测试,这始终是最好的文档确实)
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 %>
这篇关于如何使用simple_form创建分组选择框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!