如何使用 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-ing 找到它.
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 grouped select"谷歌搜索的最高结果,所以我认为下一位读者可能会从一些创造性的创建方式中受益这些带有最新的 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屋!