How to hide Add new option in Rails Admin - ruby-on-rails

I am customizing Rails Admin : https://github.com/sferik/rails_admin , i need to disable/hide "Add new" option for some model.
Any help will save lot time for me. Thanks in advance

I use the following to achieve this on a specific model. Hopefully, this helps:
config.actions do
new do
except ['Some Model']
end
end

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

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.

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

Related

Rails Admin integration with Mobility gem

I'm trying to use the Mobility gem with Rails Admin.
I am able to edit fields through the Rails Admin interface (like the impact_description of a Ngo model) and it correctly changes the translation tables:
But the problem is, the Rails Admin interface does not show the correct translation (it is showing in pt-BR, even when the app is in english):
Has anyone sucessfully integrated Rails Admin with the Mobility gem? Thanks for the attention
EDIT:
In my rails_admin.rb (initializer) I have the regular configurations for all actions (only changing new):
config.actions do
dashboard # mandatory
index # mandatory
new do
except [RewardRule, SuggestedFeed]
end
export
import
bulk_delete
show
edit
# delete
clone
show_in_app
## With an audit adapter, you can add:
# history_index
# history_show
end
And for the Ngo model I don't have any custom configuration, only that it is included on the Rails Admin models:
# rails_admin.rb
config.included_models = [..., Ngo, ...]
So I was able to do it after some research in the Rails Admin api.
For this to work on the Ngo model in the show action, for example, I did:
config.model Ngo do
show do
fields do
formatted_value{ bindings[:object].send(method_name) } # this calls the mobility method instead of getting the plain attribute, so it will translate on the admin.
end
end
end
In this case, all the fields will be calling the original method name, instead of the plain attribute. So for example, in my case, it will be calling ngo.impact_description, which will get the field translated, instead of reading the plain attribute ngo[:impact_description], which will always be in the default language.

activeadmin, remove empty message

In ActiveAdmin, when there are no items for the model (in my example User), it shows a default 'There are no Users yet. Create one'.
How can I remove this message?
Is there the possibility of customizinig it on per-page basis, that is having a particular message for a particular ActiveAdmin page?
This is a MonkeyPatch:
Create a new file in lib folder and copy:
module ActiveAdmin
module Views
# Build a Blank Slate
class BlankSlate < ActiveAdmin::Component
builder_method :blank_slate
def default_class_name
'blank_slate_container'
end
def build(content)
super(span(content.html_safe, class: "blank_slate"))
end
end
end
end
Customize the content variable in build method to change the default message.
For now you cannot do it by ActiveAdmin settings. Look issues in
repository.
You can render any html or erb.html in your resources
You may use poorly documented (but not so monkey-path) blank_slate_link index page option. It lets you override this message in any of your ActiveAdmin's resource index page.
See https://stackoverflow.com/a/72529909/1744707 for details.

Rails way to create several "create" pages

I have an "admin control panel" page which is handled by an AdminController. You can do two things in the control panel: create_product and create_order. There will be forms for each object, and when you submit the form, it will insert new records into the database.
What is the Rails way for implementing this? Do I generate a CreateProductController and CreateOrderController along with the appropriate view, which are accessible by clicking on the create_product and create_order links from the control panel?
Does the Rails way describe a way to handle both workflows using a single controller? At some point I will need to define a post method for each form, so it seems like creating separate controllers is the easiest way to set up required behavior and also the routing details.
Product and Order are all resources when having controller. The better way is to use RESTful resources. You can also add namespace for easier identification.
class Admin::ProductsController < AdminController
def create
end
def new
end
# And #show, #index, #destroy etc.
end
class Admin::OrdersController < AdminController
def create
end
def new
end
end

remove action for model in rails_admin

I have a problem. In my app I am using rails_admin gem. Everything is good except one thing. For some models I want to make possible only to delete them. Is there an easy way to do this?
In your rails_admin.rb file, you can add the default actions for your models in wich you can add exceptions as shown here.
Here is an exemple :
config.actions do
dashboard # mandatory
index # mandatory
new do
except ['SomeModel']
end
export
bulk_delete
show
edit do
except ['SomeOtherModel']
end
delete
show_in_app
end
Here is the link to the rails_admin documentation about actions : https://github.com/sferik/rails_admin/wiki/Actions
try changing in the file: config/initilizers/rails_admin.rb you can comment out the actions that you don't want to allow!
You can do this using CanCan: https://github.com/sferik/rails_admin/wiki/Cancan
add this to your ability.rb file:
cannot :manage, Model # disable all actions for this model
can :destroy, Model # enable only to remove

rails_admin how to exclude actions in models

I am using rails_admin for the first time and needed to create a new action for one of my models.
This is what I added to initializer.rails_admin.rb:
config.model Movie do
...
....
config.actions do
collection :top_five do
action_name :top_five
end
end
end
I thought this would create a new action for my Movie model, but it is present as an option (tab) for all models.
How can I make it to appear only in one model?
Thank you!
Fernanda
The only clean way I know of that currently allows this is by using CanCan for authorization, described in the RailsAdmin documentation here.
You may also override rails_admin views to show the tab only for the specific model, but that's not quite as clean.
I know this is an old question, but for everyone else landing here, here is my solution:
config.actions do
...
show_in_app
collection :top_five do
only ['Movie']
i18n_key :top_five
end
...
end
This has been added to the documentation

Resources