26

is there a way to specify in ActiveAdmin's index page of a model what actions are allowed, things like:

index do
  actions :edit
end

index do
  actions only: :edit
end

do not work. What's the correct syntax?

Appreciated.

bundle show activeadmin
/home/muichkine/.rvm/gems/ruby-2.1.2/bundler/gems/active_admin-9cfc45330e5a
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
muichkine
  • 2,890
  • 2
  • 26
  • 36

5 Answers5

35

Add whatever actions you want to be available by using actions (it is usually put under model definition):

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

If you want to specify the method for certain action, you can do

action_item only: :show  do
  link_to 'Edit', action: :edit # so link will only be available on show action
end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • 2
    Is the action `:create` or `:new`? If I use `actions :all, except: [:create]` I still get a "New" button on the edit page. Using `actions :all, except: [:new]` makes the button go away. – spinlock Feb 23 '15 at 22:04
12

Example how to play with the action column. In this example I just re-implemented the default one, but you can do powerful coding here:

column :actions do |item|
  links = []
  links << link_to('Show', item_path(item))
  links << link_to('Edit', edit_item_path(item))
  links << link_to('Delete', item_path(item), method: :delete, confirm: 'Are you sure?')
  links.join(' ').html_safe
end
Ziv Barber
  • 595
  • 9
  • 13
  • 1
    This worked for me (the selected answer didn't). Only thing is that `item_path` should be something like `admin_itemname_path` and `edit_item_path` should be `edit_admin_itemname_path`. – Nico Brenner Jul 15 '19 at 23:44
  • This is a perfect situation for .tap. `Array.new.tap do |links| { links << link_to ... }.join(' ').html_safe` – danielricecodes Aug 14 '20 at 02:02
  • for the confirm, it should be `data: { confirm: 'Are you sure?' }` – a.fahmiin Sep 08 '22 at 04:02
6

According to source code, https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/views/index_as_table.rb#L80

if one want to change the actions in the index he should go with

actions defaults: false do |sample|
  link_to t('active_admin.edit'), admin_sample_path(sample)
end

where you can replace the link title and the path for the action

For Example:

    actions defaults: false do |user|
      link_to t('active_admin.view'), admin_user_path(user)
    end

Note:

Keep in mind that add the path correctly like for show it should be admin_user_path(:id) and for index it should be admin_users_path :)

ARK
  • 772
  • 7
  • 21
muichkine
  • 2,890
  • 2
  • 26
  • 36
5

Do this way,

ActiveAdmin.register Foobar do
  actions :all, :except => [:destroy]
end

or

ActiveAdmin.register Foobar do
  actions :only => :edit
end

Need to be specified at resource level not in method definition

maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • 2
    Based on the [source code](https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/views/index_as_table.rb#L80) it should be customized in method definition too, but only with adding new actions and disabling default actions completely. – Gabor Garami Sep 08 '14 at 15:30
  • @GaborGarami, you can customize it inside individual method. Check this out http://activeadmin.info/docs/8-custom-actions.html#custom_action_items – maximus ツ Sep 08 '14 at 15:42
  • Andrey also showed how it can be customized, but I guess muichkine is only dealing with defaults actions. – maximus ツ Sep 08 '14 at 15:43
  • Actually the winning answer is Gabor's one :) – muichkine Sep 09 '14 at 08:10
  • 1
    I could not get :only to work...but I did find that "actions :index" got the job done. That is, it excluded :edit, :view, etc. – David Hempy May 23 '19 at 17:33
0

If you want multiple custom actions, instead of dealing with joining the links manually like Ziv Barber did, you can also use the item method like so:

actions defaults: true do |user|
  item "Report", report_admin_user_path(user), method: :put
  item "Unlock", unlock_admin_user_path(user), method: :put
end
KNejad
  • 2,376
  • 2
  • 14
  • 26