Discover My 3 Key Takeaways from Rails Administrate

Discover My 3 Key Takeaways from Rails Administrate

Play this article

Building an Administration Dashboard with Ruby on Rails is simple. With the gem architecture of Rails itself and the Gem `Administrate`, you can easily create a beautiful view for your super users.

To install it, just use `bundle add administrate; rails g administrate:install`

Administrate will go ahead and create dashboards for all your existing models.

My top 3 learnings + BONUS

1. Disable a specific action

Administrate checks if a specific route is existent. Let's say the edit path for a route. If it is, then the button is shown on the corresponding page. It won't be shown otherwise.

So to quickly disable an action, go to your routes.rb file and make use of the except and only feature:

# routes.rb

Rails.application.routes.draw do
  namespace :admin do
    resources :users, except: :edit
  end
end

2. Creating a new dashboard fast

Ruby on Rails embraces the philosophy of convention over configuration at its core. If you wish to create a new dashboard, take advantage of the available helpers.

rails g administrate:dashboard <model>

This will save you a lot of time and you are given the controllers.

3. Advanced Dashboards - Administrate HasMany

For a model that refers to another via the has_many attribute, you can showcase these associated resources by employing the Field::HasMany field within the dashboard class of your respective model.

With the Field::HasMany line in place, the following output will be produced.

This table is included via the Field::HasMany line

4. BONUS: Overriding templates

You can see the existing templates here: https://github.com/thoughtbot/administrate/tree/main/app/views/administrate/application.

Administrate adheres to a particular structure when searching for your views. If you wish to override a specific view, create a view file in the corresponding folder.

For example:

You will need to obtain the code from the respective GitHub repository.

I had some issues with the accessible_action? helper. I just removed it in my case.

Or simply

$ rails g administrate:views:edit <resource>

Conclusion

Administrate is an excellent gem, and once you've grasped its concepts, it becomes a fantastic time-saver.

Subscribe to my newsletter to save time by avoiding the mistakes I've already made.