【发布时间】:2017-10-05 05:06:47
【问题描述】:
如何使用表中的 simple_form 分组选项创建选择,而不是从集合中?试过了:
= f.input :countries,
:collection => [["North America",[["United States","US"],["Canada","CA"]]]],
:as => :grouped_select
但出现错误:nil 不是符号
【问题讨论】:
如何使用表中的 simple_form 分组选项创建选择,而不是从集合中?试过了:
= f.input :countries,
:collection => [["North America",[["United States","US"],["Canada","CA"]]]],
:as => :grouped_select
但出现错误:nil 不是符号
【问题讨论】:
快速查看https://github.com/plataformatec/simple_form 上的文档表明您需要在collection_select 上使用:group_method => :method。
这是他们给出的完整示例:f.input :country_id, :collection => @continents, :as => :grouped_select, :group_method => :countries
另外,如果您不知道,simple_form 有一个 country_select 助手,如果您打算使用它,您需要将 gem 'country_select' 添加到您的 gemfile。
使用该助手的 country_select 可能如下所示:
f.input :shipping_country, :priority => [ "Brazil" ] 可以选择将列表限制在几个国家,例如:f.input :shipping_country, :priority => [ "Brazil" ], :collection => [ "Australia", "Brazil", "New Zealand"] 这些示例可在“优先级”部分的 simple_form 文档中找到:http://rubydoc.info/github/plataformatec/simple_form/master/frames。显然,simple_form 会检测到它是针对一个国家/地区的,所以只需使用 input 就足够了。
【讨论】:
就我而言,仅当我使用 as: :grouped_select 和 :group_method => :last 时才有效
【讨论】: