如何将自定义过滤器添加到 Active Admin?
Active Admin 允许我定义显示在索引页面上的过滤器,如下所示:
ActiveAdmin.register Promo do
filter :name
filter :address
filter :city
filter :state
filter :zip
end
我想将上述所有字段合并为一个,以便我可以搜索名称或完整地址中包含搜索字符串的促销。我的模型已经有一个可以使用的命名范围:
class Promo < ActiveRecord::Base
scope :by_name_or_full_address, lambda { |q| where('name LIKE :q OR address LIKE :q OR city LIKE :q OR state LIKE :q OR zip LIKE :q', :q => "%#{q}%") }
end
Active Admin allows me to define filters that are displayed on the index page like so:
ActiveAdmin.register Promo do
filter :name
filter :address
filter :city
filter :state
filter :zip
end
I would like to combine all the fields above into one, so that I can search for Promos that contain the search string in name or full address. My model already has a named scope that I can use:
class Promo < ActiveRecord::Base
scope :by_name_or_full_address, lambda { |q| where('name LIKE :q OR address LIKE :q OR city LIKE :q OR state LIKE :q OR zip LIKE :q', :q => "%#{q}%") }
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
主动管理使用元搜索。例如你可以这样做:
Active admin uses metasearch. For example you can do this:
Active Admin 使用 meta_search gem 作为其过滤器。 ORed 条件语法允许在一个查询中组合多个字段,例如
在 Active Admin DSL 中,这会转换为
Active Admin uses the meta_search gem for its filters. ORed conditions syntax allows to combine several fields in one query, for example
In Active Admin DSL this translates to
在较新版本的 ActiveAdmin 中执行此类过滤的另一种方法:
然后在模型文件中添加以下 2 个函数
您的过滤逻辑:
为 Ransack 注册新过滤器:
Another way of doing such filtering in newer version of ActiveAdmin:
Then add following 2 functions in your model file
Your filtering logic:
Registering new filter for Ransack :
要使用自定义过滤器,您可以创建范围函数并将其添加为模型中的 search_methods。
例如,在我的 User 模型上:
然后在 users.rb 中,我可以使用我的范围作为自定义过滤器:
To use a custom filter, you can create a scope function and add it as search_methods in the model.
For example, on my User model:
Then in users.rb, I can use my scope as a custom filter:
2018 年回答。ActiveAdmin 使用 Ransack。
在模型本身上,您需要添加 Ransack 格式化程序:
在 ActiveAdmin 文件中,您需要指定规则:
Answering in 2018. ActiveAdmin uses Ransack.
On model itself you need to add Ransack formatter:
In ActiveAdmin file you need to specify the rule:
我找到了更好的方法。您只需要添加:
然后使用构建器
ActiveAdmin::FormBuilder
在_search
部分中制作表单,就像它在:https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb
有关如何执行此操作的更多信息,请查看以下要点:
https://gist.github.com/4240801
另一个想法是创建类:
它将能够通过
as: :custom_string
调用,但我不喜欢这个想法,因为您很快就会发现,您需要创建 custom_select 等等...I found better way of doing that. You just need to add:
And then make form inside the
_search
partial with the builderActiveAdmin::FormBuilder
as it did in:https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb
For more information how to do it, look to this gist:
https://gist.github.com/4240801
Another idea is to create class:
that will be able to invoke by
as: :custom_string
, but I don't like that idea, because you can find soon, that you will need to create custom_select and so on...我有属于 User 模型的模型 WithdrawalRequest 。
要通过用户的电子邮件过滤提款请求,需要编写:
I have model WithdrawalRequest which belongs to User model.
For filtering withdrawal requests by user's email need write:
这对我有用:
在我的模型中
在我的管理文件中
filter :listing_status_in, as: :select, collection: %w(Active Inactive), label: 'Listing Status'
This worked for me:
In my model
In my admin file
filter :listing_status_in, as: :select, collection: %w(Active Inactive), label: 'Listing Status'