How to hide Add new option in Rails Admin
Asked Answered
B

4

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

Basal answered 12/8, 2013 at 15:45 Comment(0)
R
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
Rattoon answered 20/8, 2013 at 22:16 Comment(3)
hello, but if in the url i add action, this allow access. Example http://localhost:3000/dashboard/user/new :/Bore
@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.Dorsum
Just to be sure, you need to use a class name without single quotes. like except [User]Schwa
V
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
Viewpoint answered 12/8, 2013 at 16:2 Comment(5)
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.Basal
In that case, I’m afraid I don’t know how to do it. Maybe some other part of the RailsAdmin documentation explains how. Maybe you have to look at the source code to see how to do it – this part might be relevant. Or maybe RailsAdmin does not support that feature.Gerigerianna
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.Gerigerianna
Cool, Its not possible only with Rails_Admin. We can achieve it easily using CAN-CAN , thanks for the your time.Basal
@Basal Now that you mention CanCan, I remember that the documentation has example can calls related to the new action, though I hadn’t realized that that was relevant.Gerigerianna
M
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.
Maher answered 26/11, 2018 at 21:35 Comment(0)
B
-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

Basal answered 12/8, 2013 at 17:37 Comment(1)
This is selected as the answer, but it is not true. See my answer above.Rattoon

© 2022 - 2024 — McMap. All rights reserved.