如何添加条件以在 ActiveAdmin 中显示

How to add a conditional to show in ActiveAdmin

我有一个任务。 显示不同的字段取决于我的对象参数。

例如,对于我的表单,如果当前对象的 message_type = 'custom' 如果没有,我将显示一个输入 - 另一个。

这是现在有效的代码:

  form do |f|
    if f.object.new_record? || f.object.message_type == 'custom'
    f.inputs do
        f.input :custom_topic
        f.input :custom_content, as: :text
      end
    f.actions
    end
  end

但为了展示我不知道如何检查它。我现在拥有的:

  show do
    attributes_table do
      if :message_type == 'custom'
        row :message_type
        row(:topic) { |object| object.notification_data['topic'] }
        row(:content) { |object| object.notification_data['content'] }
      else
        row :message_type
        row :notification_data
       end
    end
  end

当我 运行 调试器显示

message_type={符号}

我同意)

但是如何检查当前对象的 message_type 值?

我的所有代码如下:

ActiveAdmin.register Notification do
  belongs_to :case, finder: :find_by_slug
  permit_params :message_type, :custom_topic, :custom_content, :notification_data

  form do |f|
    if f.object.new_record? || f.object.message_type == 'custom'
    f.inputs do
        f.input :custom_topic
        f.input :custom_content, as: :text
      end
    f.actions
    end
  end

  show do
    attributes_table do
      if :message_type == 'custom'
        row :message_type
        row(:topic) { |object| object.notification_data['topic'] }
        row(:content) { |object| object.notification_data['content'] }
      else
        row :message_type
        row :notification_data
       end
    end
  end

  config.filters = false
end

show 块中,当前资源作为通常以您管理的模型命名的方法提供。在这种特殊情况下,它可能是 notification,因此以下可能有效:

  show do
    attributes_table do
      if notification.message_type == 'custom'
        row :message_type
        row(:topic) { |object| object.notification_data['topic'] }
        row(:content) { |object| object.notification_data['content'] }
      else
        row :message_type
        row :notification_data
       end
    end
  end