ruby-on-railsruby-on-rails-4ruby-on-rails-5activeadminruby-on-rails-6

ActiveAdmin hide Delete action by condition


I have some problem.

In ActiveAdmin I need hide DELETE action by condition.

I did it for #index page. But I don't know how do this trick with #show page.

Here's code:

index do
    selectable_column
    column :id do |package|
      link_to package.id, admin_subscription_package_path(package)
    end
    column :title
    column :plan_status
    column :duration do |package|
      if package.duration == 1
        "#{package.duration} day"
      else
        "#{package.duration} days"
      end
    end
    column 'Price (USD)', :price do |package|
      number_to_currency(package.price, locale: :en)
    end
    column :actions do |object|
      raw(
          %(
            #{link_to 'View', admin_subscription_package_path(object)}
            #{(link_to 'Delete', admin_subscription_package_path(object),
                       method: :delete) unless object.active_subscription? }
            #{link_to 'Edit', edit_admin_subscription_package_path(object)}
          )
      )

    end
  end

Or maybe I can do it more useful for all pages at once.


Solution

  • Use action_item for this purpose:

    ActiveAdmin.register MyModel
    
      actions :index, :show, :new, :create, :edit, :update, :destroy
    
      action_item only: :show  do
        if condition
          link_to "Delete whatever", {action: :destroy}, method: :delete, confirm: 'Something will be deleted forever. Sure?'
        end
      end
    
    end