如何将自定义过滤器添加到Active Admin? [英] How to add custom filter to Active Admin?

查看:81
本文介绍了如何将自定义过滤器添加到Active Admin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Active Admin允许我定义索引页面上显示的过滤器像这样:

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

我想将上面的所有字段合并为一个,以便我可以搜索包含名称或完整地址中搜索字符串的Promos。我的模型已经具有我可以使用的命名范围:

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


推荐答案

Active Admin使用 meta_search 过滤器。 ORed条件语法允许在一个查询中组合多个字段,例如

Active Admin uses the meta_search gem for its filters. ORed conditions syntax allows to combine several fields in one query, for example

Promo.metasearch(:name_or_address_contains => 'brooklyn')

在Active Admin DSL中,这表示为

In Active Admin DSL this translates to

ActiveAdmin.register Promo do

  filter :name_or_address, :as => :string

end

这篇关于如何将自定义过滤器添加到Active Admin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆