49

in rails gem active admin I want to remove the delete option form the default_actions while I still need the edit and show action , is there any way to do it ?

Kareem Hashem
  • 1,057
  • 1
  • 9
  • 25

5 Answers5

110

You add a call to actions to every Active Admin resource:

ActiveAdmin.register Foobar do
  actions :all, :except => [:destroy]
end
Raj
  • 22,346
  • 14
  • 99
  • 142
Thomas Watson
  • 6,507
  • 5
  • 33
  • 43
10

At some point I had this problem, because of the destroy method, the 'Delete' button didn't disappear

actions :all, except: [:destroy]

controller do
  def destroy # => Because of this the 'Delete' button was still there
    @user = User.find_by_slug(params[:id])
    super
  end    
end
vladCovaliov
  • 4,333
  • 2
  • 43
  • 58
3

The accepted answer threw an exception, "wrong number of arguments" so I did this to exclude the delete button(:destroy action)

ActiveAdmin.register YourModel do
  actions :index, :show, :new, :create, :update, :edit

   index do

     selectable_column
     id_column
     column :title
     column :email
     column :name

    actions 
   end
Means
  • 322
  • 2
  • 4
  • 16
2

Another way to remove actions from default_actions for an ActiveAdmin resource is via config variable, like:

    ActiveAdmin.register MyUser do
      config.remove_action_item(:destroy)
      ...
    end

One way is already mentioned in the accepted answer via actions method.

Manoj Sehrawat
  • 1,283
  • 1
  • 10
  • 25
0

If you want to remove the remove the destroy button completely use:

actions :all, except: [:destroy]

But if the delete button needs condition based on the resource properties.(e.g. associated data or status).

At index page:

index do

  # ...

  actions defaults: false do |row|
    if can? :read, row
      text_node link_to "View", admin_resource_path(row), class: "view_link"
    end
    if can? :edit, row
      text_node link_to "Edit", admin_resource_path(row), class: "edit_link"
    end
    if can? :destroy, row
      text_node link_to I18n.t('active_admin.delete'), admin_resource_path(row), method: :delete, data: { confirm: I18n.t('active_admin.delete_confirmation') }, class: "delete_link" if row.deletable?
    end
  end
end

Now the complex part and i had to bang my head several times to control it at show page:

config.remove_action_item(:destroy) # will remove the destroy button
  
action_item only: :show  do
  link_to I18n.t('active_admin.delete'), admin_resource_path(resource), method: :delete, data: { confirm: I18n.t('active_admin.delete_confirmation') }, class: "delete_link" if resource.deletable?
end

Sorry for my terrible formatting.

jordelver
  • 8,292
  • 2
  • 32
  • 40
cool_php
  • 319
  • 7
  • 10