简体   繁体   中英

ActiveAdmin custom select filter dropdown names

For a user model I have a filter to check the booking status of a user which is represented by an integer value (0, 1, or 2).

The filter on the User ActiveAdmin index page is achieved with the following code:

  filter :booking_status, as: :select

However this results in the dropdown options being either 0, 1, or 2.

I would prefer if I could name them myself to something like "Incomplete", "Pending", and "Confirmed" when the admin user is selecting them from the dropdown.

在此输入图像描述

Is there any way of doing this without changing how the booking_status is represented in the model?

Assuming booking_status is an enum field in your model, you can use:

filter :booking_status, as: :select, collection: ModelName.booking_statuses

If booking_status is not an enum field, you can pass a regular hash to the collection option, something like:

filter :booking_status, as: :select, collection: {'Incomplete' => 0, 'Pending' => 1, 'Complete' => 2}

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