rails:活动的管理员过滤器
rails: Active admin filter
我在我的 rails 应用程序中使用 Active admin 并希望根据国家/地区过滤公司模型,我在公司 table 中有一个数据类型为字符串的国家/地区列,并且我' m 使用 country-select
gem。
所以我实际上在做的是
filter :country, as: :country
但这行不通谁能帮我弄一个工作国家过滤器?
我知道此代码 filter :country, as: :string
可以使用,但我想要一个包含所有国家/地区的 select 列表
这就是您在 AA 中使用过滤器的方式:
filter :country, as: :select
它的作用是为 select 创建一个集合,其中 Company
class 的每个实例都由名为 country
的属性映射
编辑
根据评论,要使用国家名称而不是代码,您可以使用以下内容:
filter :country, as: :select, collection: ActionView::Helpers::FormOptionsHelper::COUNTRIES
编辑#2
要在视图中显示国家名称而不是国家代码,您必须执行以下操作:
column :country do |g|
ISO3166::Country[g.country]
end
我有一个名为 Soundwave 的模型,并且有一个国家字段,只包含两个国家:英国和美国。
所以,我做了一个这样的集合:
filter :country, as: :select, collection: -> {
Soundwave.group(:country).select(:country).map(&:country)
}
我在我的 rails 应用程序中使用 Active admin 并希望根据国家/地区过滤公司模型,我在公司 table 中有一个数据类型为字符串的国家/地区列,并且我' m 使用 country-select
gem。
所以我实际上在做的是
filter :country, as: :country
但这行不通谁能帮我弄一个工作国家过滤器?
我知道此代码 filter :country, as: :string
可以使用,但我想要一个包含所有国家/地区的 select 列表
这就是您在 AA 中使用过滤器的方式:
filter :country, as: :select
它的作用是为 select 创建一个集合,其中 Company
class 的每个实例都由名为 country
编辑
根据评论,要使用国家名称而不是代码,您可以使用以下内容:
filter :country, as: :select, collection: ActionView::Helpers::FormOptionsHelper::COUNTRIES
编辑#2
要在视图中显示国家名称而不是国家代码,您必须执行以下操作:
column :country do |g|
ISO3166::Country[g.country]
end
我有一个名为 Soundwave 的模型,并且有一个国家字段,只包含两个国家:英国和美国。 所以,我做了一个这样的集合:
filter :country, as: :select, collection: -> {
Soundwave.group(:country).select(:country).map(&:country)
}