14

I am customizing Rails Admin : https://github.com/sferik/rails_admin , i need to disable/hide "Add new" option for some model.

enter image description here

Any help will save lot time for me. Thanks in advance

Senthil
  • 946
  • 1
  • 14
  • 34

4 Answers4

27

I use the following to achieve this on a specific model. Hopefully, this helps:

config.actions do
  new do
    except ['Some Model']
  end
end
RubeOnRails
  • 1,153
  • 11
  • 22
  • hello, but if in the url i add action, this allow access. Example `http://localhost:3000/dashboard/user/new` :/ – kalelc Jan 10 '15 at 18:49
  • 1
    @andres using the above rails_admin DSL will only remove the buttons from the admin. As long as the routes still exist for that action, you will be able to do that action. – Josh Click Feb 04 '15 at 22:09
  • Just to be sure, you need to use a class name without single quotes. like `except [User]` – Pramod Solanky Oct 01 '15 at 12:10
3

The answer is in the configuration documentation for actions. By default, all actions are possible, including new. To customize the possible actions, in config.actions in config/initilizers/rails_admin.rb, list all the actions you want to support, leaving out the ones you don’t want to support. For example, here is a config block that allows all of the default actions except for new:

# config/initilizers/rails_admin.rb
RailsAdmin.config do |config|
  config.actions do
    # root actions
    dashboard
    # collection actions 
    index
    # `new` is NOT allowed
    export
    history_index
    bulk_delete
    # member actions
    show
    edit
    delete
    history_show
    show_in_app
  end
end
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • Hi Rory , thanks for quick answer. Since it was in initializers its applied for all models. It hides all models new action , but I want it to hide new action in specific model. – Senthil Aug 12 '13 at 16:53
  • In that case, I’m afraid I don’t know how to do it. Maybe some other part of the [RailsAdmin documentation](https://github.com/sferik/rails_admin/wiki) explains how. Maybe you have to look at the [source code](https://github.com/sferik/rails_admin) to see how to do it – [this part](https://github.com/sferik/rails_admin/blob/master/app/controllers/rails_admin/main_controller.rb#L15-L26) might be relevant. Or maybe RailsAdmin does not support that feature. – Rory O'Kane Aug 12 '13 at 16:59
  • If necessary, there might be some hack that lets you run the initializer multiple times, letting you disable the `new` action when doing anything with that model and reenabling it when accessing any other model. – Rory O'Kane Aug 12 '13 at 16:59
  • Cool, Its not possible only with Rails_Admin. We can achieve it easily using CAN-CAN , thanks for the your time. – Senthil Aug 12 '13 at 17:36
  • @Senthil Now that you mention CanCan, I remember that the documentation has [example `can` calls related to the `new` action](https://github.com/sferik/rails_admin/wiki/New-action), though I hadn’t realized that that was relevant. – Rory O'Kane Aug 12 '13 at 19:12
1

To have multiple models, you must put each model in single quotes. For example, consider the following configuration:

config.actions do
  dashboard
  index do
    except ['Address']
  end
  new do
    except ['Address', 'Employee', 'Setting']
  end
  export
  show
  edit do
    except ['Employee']
  end
end

This means that:

  • Addresses are not included on the navbar on the left
  • You cannot add a new address employee or setting with the "add new" button
  • There is no pencil icon in the index view for Employees for editing.
  • If you had a User model you could see it in the navbar, edit it, and add a new one on the index page.
  • You can export every model, but not bulk delete them.
danielsmith1789
  • 1,056
  • 1
  • 12
  • 25
-3

Implemented it with Cancan. You can refer to above answer to do it in rails admin way.

URL : https://github.com/sferik/rails_admin/wiki/CanCan

Senthil
  • 946
  • 1
  • 14
  • 34