Rails | Active Admin

Jordan Eckowitz
4 min readDec 10, 2018

This week I’ve decided to step away from my usual venture into the world of Javascript and will delve into an incredibly useful Rails Gem called Active Admin. With just a few simple lines of code you can create an admin route that provides powerful insight into the models and data in your Rails projects. Let’s get stuck in!

Setting up Our Rails Example

For the example, we’re going to make a simple stock trading app. We’ll have three models: Users, Portfolios and Stocks. The user can have many trading portfolios as well as many stocks through the portfolios. The portfolios belong to the users, the stocks belong to the portfolios.

rails new active_admin_example
rails g model User
rails g model Portfolio
rails g model Stock

Our models will look like this:

Let’s now create some simple seed data so we’ve got something to show on our admin page.

Setting Up Active Admin

To add Active Admin to your project add the following to your Gemfile. Devise is a Gem that handles authentication for Rails. If you’re incorporating Active Admin to an existing project there’s a good chance you already have Devise installed.

gem 'activeadmin'
gem 'devise'

Now all we need to do to incorporate Active Admin is to use our ever-faithful generator as follows. This will create an AdminUser class to use with Devise.

rails g active_admin:install

Now all we’ve got to do is register our models with Active Admin. The general syntax is rails generate active_admin:resource [Model Name].

rails generate active_admin:resource User
rails generate active_admin:resource Portfolio
rails generate active_admin:resource Stock

After all the generation is complete we’ll have an admin folder with files for our generated models as well as for the admin_users and dashboard. More on dashboard in a minute.

We’re 3 lines away from getting Active Admin up and running. We first create our migration. There will be 2 Active Admin migrations. One for Devise for handling Admin Users and another for Active Admin comments. Comments provides the incredibly useful ability to add notes about any entry in any of your linked models. This is really powerful as it allows administrators to track issues.

rails db:migrate
rails db:seed
rails server

It’s also useful to note that the following code is auto-generated within the seeds file. This gives default admin login with the shown credentials. Feel free to change the email and password before seeding. You can also update this later within the Active Admin interface.

After starting up our server go to http://localhost:3000/admin or tweak the URL if you’re running on a different port.

After logging in with the email and password from above we get the following interface. Easy as pie. We can now inspect any of our data, filter the displayed results and even export to CSV without having to write up any further code. There’s routes on the navigation bar for all of our linked models. I don’t know about you but I think that’s amazing!

The dashboard page is blank to begin with but you can create useful custom statistics, graphs and tables that provide a high-level overview of your data.

Active Admin allows you to create a range of different view components that you can add to your dashboard. I’ll keep it simple and add a component to the dashboard called a panel. A panel is a component that takes up all available horizontal space and takes a title and a hash of attributes as arguments. If a sidebar is present, a panel will take up the remaining space.

Here’s a simple, contrived example, for a panel that shows all portfolio titles within the database. Add the following code to dashboard.rb.

Here you can see our panel showing up on the Dashboard.

Conclusion

Active Admin can scaffold an incredibly powerful, production-ready admin portal by barely lifting a finger. An incredibly powerful addition to any Rails programmer’s toolbelt.

--

--