Tom Prats
Tom Prats

Reputation: 7911

Active admin action item/member action

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

Upvotes: 12

Views: 20917

Answers (4)

Piyush Chaudhary
Piyush Chaudhary

Reputation: 313

Under the index do write

actions dropdown: true do |category|
  item 'Archive', archive_admin_post_path(post)
end

Upvotes: 1

imsinu9
imsinu9

Reputation: 769

For the friend who landed the page, In order to append more than 1 link

Do Something Like:

    actions default: true do |model|
        [
            link_to('Option 1', "#"),
            '  ',
            link_to('Option 2', "#")
        ].reduce(:+).html_safe
    end

Upvotes: 7

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

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.

Upvotes: 40

Tom Prats
Tom Prats

Reputation: 7911

Found an answer here. You can do it using the below code with the code from the question (removing the action item block)

index do
...
  actions do |subscription|
    link_to('Approve', approve_admin_subscription_path(subscription))
  end
...
end

But I think there is a way to do it by appending an action to the default actions (so if you know how to do that, then add another answer!)

Additionally to remove the defaults you can change it like it is here:

actions :defaults => false do |subscription|

Upvotes: 3

Related Questions