简体   繁体   中英

How to search for a substring in a custom filter in ActiveAdmin?

In the past:

A model User has a string filed first_name . If I write filter :first_name in ActiveAdmin model it would display a drop-down besides the input field with the options Contains , Equals , Starts with , Ends with . So for examle if I choose Contains and write ik , it would find, for example Mike.

Now:

If I use a custom filter I just get a large input, spanning across two columns, which only does exact search. I define my filter like so:

  1. Create a scope in the model.
  2. Whitelist it with ransackable_scopes .
  3. Add it to the admin model like so: filter :my_filter, as: :string .

How do I get the functionality to seach for substrings?

To search for substrings you must use the Ransack Search Predicate Syntax ie if you want search a substring on field first_name you must configure this way:

filter :first_name_cont, as: :string, label: "Name"

You can find more information here

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM