a.kozubenko
a.kozubenko

Reputation: 1060

How to add a conditional to show in ActiveAdmin

I have a task. Show a different field depends of my object parameters.

For example for my form if message_type of current object = 'custom' I will show one inputs if not - another.

Here is a code what works now:

  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

But for show I do not know how to check it. What I have now:

  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

When I run debugger it shows me

message_type={Symbol}

and I'm agree)

But how to check value of message_type for current object?

My all code bellow:

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

Upvotes: 2

Views: 5707

Answers (1)

eugen
eugen

Reputation: 9226

In the show block the current resource is available as a method usually named after the model you're managing. In this particular case it's probably notification, so the following might work:

  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

Upvotes: 4

Related Questions