Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 234
Tom Prats Avatar asked Sep 16 '13 20:09

Tom Prats


People also ask

What is member action in Rails?

A member action is a controller action which operates on a single resource. For example, to add a lock action to a user resource, you would do the following: register User do member_action :lock, method: :put do resource. lock! redirect_to resource_path, notice: "Locked!"

What is active admin?

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.


3 Answers

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|
like image 26
Tom Prats Avatar answered Oct 18 '22 02:10

Tom Prats


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.

like image 169
Pierre-Louis Gottfrois Avatar answered Oct 18 '22 03:10

Pierre-Louis Gottfrois


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
like image 30
imsinu9 Avatar answered Oct 18 '22 01:10

imsinu9