如何使用关联的 ActiveAdmin 显示值的文本查找

How to show text lookup for a value with ActiveAdmin show on an association

我有一个 User 模型 has_one subscription_type。我正在尝试显示 subscription_type 中的字段之一,但它的值为整数 (0, 1),我想显示相应的文本(免费、高级)。

目前:

  show do |user|
    default_main_content
    panel 'subscription' do
      attributes_table_for user.subscription_type do
        row :subscription_type
      end
    end
  end

这工作正常,但它在这里显示 :subscription_type 的整数值。

我在 SubscriptionType 模型上有一个散列,可用于从值中查找键并将其人性化:

SubscriptionType::TYPES.key(1).to_s
=> "premium"

但我一辈子都弄不清楚如何在此处的行定义中实际使用该查找。

您可以将 SubscriptionType::TYPES.key(subscription_type).to_s 作为块传递给行

  show do |user|
    default_main_content
    panel 'subscription' do
      attributes_table_for user.subscription_type do
        row :subscription_type do
          SubscriptionType::TYPES.key(user.subscription_type).to_s
        end
      end
    end
  end