12

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

4 Answers 4

40

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.

3
  • I can't test this right now but I'm going to accept cuz it looks like it'd work well
    – Tom Prats
    Oct 22, 2013 at 15:09
  • 1
    How to add more than one actions? like Approve and Reject without using more do-end..?
    – Breen ho
    Mar 20, 2014 at 13:50
  • 3
    Use concatenation. All you need to make sure is that you return safe html: (link_to ... + link_to ...).html_safe Mar 20, 2014 at 16:14
7

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
0
3

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|
1

Under the index do write

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.