DEV Community

Cover image for How to customize the display name of dynamic ActiveAdmin search filters
Juan Ferrari
Juan Ferrari

Posted on • Edited on • Originally published at rootstrap.com

How to customize the display name of dynamic ActiveAdmin search filters

This post was originally created for the Rootstrap blog

Working with ActiveAdmin

At Rootstrap, my first assignment was to develop a Ruby on Rails application which uses ActiveAdmin (with ActiveAdmin Addons) as a framework to implement the user interface.

I had never used ActiveAdmin before and I found myself reading a lot of documentation of the gem, in some cases, the documentation was not enough. For example, when I tried to display a custom name on an AJAX's search select filter at first it was a little tricky. So today I will show you how to do it, in the end, it's really simple!

Showing our custom display_name

In this tutorial, you will learn how to customize the display_name of a search_select the filter provided by ActiveAdmin Addons.

Before starting we have to include in our Gemfile the activeadmin gem and activeadmin-addons gem.

In this example we are going to use a search select to filter people and search a person, our person model has two fields named first_name and last_name, and we want to display the full name of the person when we are searching it. The full_name is the concatenation of first_name and last_name.

Steps to show our custom display_name

Create a Search Select

First of all, you should create a search select input in ActiveAdmin. This can be used in many places but in this example, we are going to use it in a filter. To do that, go to an ActiveAdmin view and put this code on it:

      filter :person_id,
             as: :search_select_filter,
             fields: %i[first_name last_name],
             display_name: :full_name

(If you are going to use this on a form you have to change search_select_filter to search_select).

The fields array indicates the fields that are used to filter, our input will be matched with the first_name of a person or their last_name.

The display_name is the field that our filter is showing to us meanwhile we search.

Setting up the returned JSON

The search select input is requesting a JSON meanwhile the user is searching for a person, for example in this case the filter will be doing requests to this endpoint /admin/people and retrieving a JSON showing all model attributes from a person. This is auto-generated by ActiveAdmin, but we can overwrite the response.

In this application we are using JBuilder to render JSON response so we have to create a file in:

views/admin/(model_name_pluralized)/index.json.jbuilder

In our example, the view created is in

views/admin/people/index.json.jbuilder

In this file we can write the custom JSON that we want to retrieve for example:

json.array!(@people) do |person|
  json.extract! person, :id, :full_name
end

Attention: full_name is a method on person model, we have to write that method in the model and not in a decorator because if we are using this search_select in edit/new view an error will raise when validation fails, ActiveAdmin will call a method from person model named full_name and not from the decorator.

Writing the full_name method

Just write the method that we want in app/models/person.rb:

def full_name
    [first_name, last_name].compact.join(' ')
end

And now our search select input is ready to display the person full name!

What we learned about Search Select Inputs?

In this article, we learned how to display a custom field in our search select inputs. This will help us to give better search feedback to the user and it can be used in many different scenarios. It also helps us take control of the JSON returned by ActiveAdmin to the input (by default, all attributes are returned and maybe that's not good for us).

References

Select2_Search ActiveAdmin

Top comments (0)