Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active admin action item not showing

I am following active_admin's documentation on action items trying to add an "approve" action to my admin view.

I have my activeadmin register set up like this:

ActiveAdmin.register PendingClaim do


  action_item :approve, method: :post, only: [:show, :index] do
    link_to('Approve', "#")
  end


  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
  end



  controller do
    # This code is evaluated within the controller class

    def approve
      binding.pry
    end
  end
end

but it doesn't show the approve action in the table. I want the approve action to map to the #approve action in the PendingClaim controller. Not sure what I have to do here...

I also tried adding actions to my index like so:

  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
    actions
  end

but that just showed the default actions without my custom approve action

EDIT --

Based on @Omnigazer's answer, I changed my code to

ActiveAdmin.register PendingClaim do


  member_action :approve, only: :index do
    redirect_to resource_path, notice: "Approved!"
  end


  index do
    column "Business ID", :business_id
    column "User ID", :user_id
    column "Claim approved by Admin?", :approved
    column :created_at
  end



  controller do
    # This code is evaluated within the controller class

    def approve
      binding.pry
    end
  end
end

but that still doesn't show the action.

EDIT --

Thanks to Omnigazer, I have managed to get it working, my code:

ActiveAdmin.register PendingClaim do
  member_action :approve, method: :post, only: :index do
  end

  index do
    column :created_at
    column 'Business ID', :business_id
    column 'User ID', :user_id
    column 'Claim approved by Admin?', :approved
    actions defaults: false do |pending_claim|
      params = { business_id: pending_claim.business_id,
                 user_id: pending_claim.user_id }
      link_to('Approve', approve_admin_pending_claim_path(pending_claim, params), method: :post)
    end
  end

  controller do
    # This code is evaluated within the controller class

    def approve
      business = Business.find(params[:business_id])
      user = Business.find(params[:user_id])
      business.user_id = user.id
      business.verified = true
      if business.save
        resource.approved = true
        resource.save
        redirect_to resource_path(resource), notice: 'Claim Approved!'
      end
    end
  end
end
like image 485
snowflakekiller Avatar asked Sep 26 '22 05:09

snowflakekiller


1 Answers

The actions items appear in the upper right corner of the page, where the "create %{model_name%}" button usually resides. Try looking for it there. Otherwise, your code looks valid. Although ActiveAdmin has its own DSL methods "collection_action" and "member_action" for cases like yours. Try looking them up in the docs.

EDIT: If you want to append an action item next to the default actions, try this:

index do
  ...
  actions defaults: true do |order|
    link_to('Approve', "#")
  end
end
like image 104
Omnigazer Avatar answered Sep 28 '22 07:09

Omnigazer