如何将自定义过滤器添加到Active Admin?

编程入门 行业动态 更新时间:2024-10-28 20:27:04
本文介绍了如何将自定义过滤器添加到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?

本文发布于:2023-11-09 17:25:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   过滤器   如何将   Admin   Active

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!