YuKagi
YuKagi

Reputation: 2501

Activeadmin disabling the "new resource" method

I'm using Activeadmin for the admin interface on an app I'm working on (loving it) and I am curious if there is a way to disable the "New Resource" link in the upper-right corner of the resource show page?

The particular resource I'm using is nested inside another resource and I have a partial that allows it to be created from the show page on that parent resource.

I have disabled the resource in the menu, but I'd rather leave the resource in the menu so I can see/edit/delete those resources without having to find it by looking through its parent resource.

Upvotes: 41

Views: 28321

Answers (9)

Magzy
Magzy

Reputation: 83

config.clear_action_items! does only half of the job. There is one issue though.

In case of empty index table, active admin show this message

There are no [Resources] yet. Create one

which doesn't get hidden by the above command and I don't want to entirely disable the action. So, I kept the link and edited the new action to redirect to the parent resource index with a message.

controller do
  def new
    if params[:parent_id].present?
      super
    else
      redirect_to parent_resources_path, notice: "Create Resource through ParentResource"
    end
  end
end

Upvotes: 1

makvool
makvool

Reputation: 412

Try config.clear_action_items! to remove the link to New and other links on top of the table

Upvotes: 38

jpbalarini
jpbalarini

Reputation: 1172

I did this:

controller do
  def action_methods
    if some_condition
      super
    else
      super - ['new', 'create', 'destroy']
    end
  end
end

To disable some of the possible actions. action_methods returns an array of the 7 standard CRUD actions, so you can subtract those you don’t want

Upvotes: 7

hcarreras
hcarreras

Reputation: 4612

config.clear_action_items!

Will remove all the actions. If you only want to remove the new action link you can also use:

config.remove_action_item(:new)

Upvotes: 15

Pedro Rodrigues
Pedro Rodrigues

Reputation: 91

I know this is an old question, but I just came up to it (had the same problem), and realized that config.clear_action_items! and actions :all, :except => [:new] are fundamentally different.

config.clear_action_items! will remove the New button from the index page, while actions :all, :except => [:new] will remove both the button, AND the route, meaning you can't call it from another place (which, in my case, is needed).

Upvotes: 9

ea0723
ea0723

Reputation: 806

This removed the "New Resource" button from the top-right:

    config.clear_action_items!

This removed both the "New Resource" button as well as the box "There are no resources yet - create one".

    actions :all, :except => [:new]

Thank you, Irio

Upvotes: 28

irio
irio

Reputation: 1401

Or even:

ActiveAdmin.register Purchase do
  config.clear_action_items!
  actions :index
end

Upvotes: 4

Balius
Balius

Reputation: 9

Worked for me too ! :-) 

ActiveAdmin.register AssetSumView do
             menu :label => "Asset Summary View", :parent => "Things"
# no button for NEW (since this is a db view)
#---------------------------------------------------------------------------------------------
config.clear_action_items!

    enter code here

   action_item do
      link_to "Assets" , "/admin/assets" 
    end

   action_item do
      link_to "AssetCatgCodes", "/admin/asset_catg_codes"
    end

#---------------------------------------------------------------------------------------------

Upvotes: 0

makaroni4
makaroni4

Reputation: 2281

Previous solution didn`t work for me, so here is general solutions, that works always:

ActiveAdmin.register Book do
  actions :index

  #or like that
  #actions :all, :except => [:destroy]

  index do
    column :title
    column :author
  end  
end

Upvotes: 70

Related Questions