简体   繁体   English

ActiveAdmin自定义选择筛选器下拉列表名称

[英]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). 对于用户模型,我有一个过滤器来检查用户的预订状态,该状态由整数值(0,1或2)表示。

The filter on the User ActiveAdmin index page is achieved with the following code: User ActiveAdmin索引页面上的过滤器使用以下代码实现:

  filter :booking_status, as: :select

However this results in the dropdown options being either 0, 1, or 2. 但是,这会导致下拉选项为0​​,1或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? 有没有办法这样做而不改变在模型中表示booking_status的方式?

Assuming booking_status is an enum field in your model, you can use: 假设booking_status是模型中的枚举字段,您可以使用:

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: 如果booking_status不是枚举字段,则可以将常规哈希传递给collection选项,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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