Active admin action item/member action

Tom Prats picture Tom Prats · Sep 16, 2013 · Viewed 16k times · Source

So I have a batch action which works wonderfully. But now I want the same action to be able to be used by clicked a button on the right (like view/edit/destroy). But I can't seem to get it right.

# app/admin/subscriptions.rb
ActiveAdmin.register Subscription do
...
  batch_action :approve_selected do |subscriptions|
    subscriptions.each do |subscription|
      Subscription.find(subscription).approve
    end
    redirect_to :back
  end

  member_action :approve do
    Subscription.find(params[:id]).approve
    redirect_to :back
  end

  action_item :only => :show do
    link_to('Approve', approve_admin_subscription_path(subscription))
  end
...
end

No button shows up next to View, Edit, or Delete with this code. I figured it was because I'm using :only => show so I taking it off or using only :index but both give the following error (and I haven't been able to find much about it):

undefined local variable or method `subscription' for #<ActiveAdmin::Views::ActionItems:0x007fb3a95b25c0>

If I change the action item line to action_item :only => index do |subscription| then that gives the following error and puts subscription just gives some html (no idea why):

undefined method `each_byte' for nil:NilClass

Answer

Pierre-Louis Gottfrois picture Pierre-Louis Gottfrois · Oct 22, 2013

This can be done with the following:

ActiveAdmin.register Post do
  index do
    column :name
    actions defaults: true do |post|
      link_to 'Archive', archive_admin_post_path(post)
    end
  end
end

Note that using defaults: true will append your custom actions to active admin default actions.