Adding a back-end with Active Admin

Created by Rasmus Kjellberg

This guide assumes that you have already built a Rails Girls app by following the app development guide.

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort. You can read more about Active Admin here.

Adding the “Active Admin” gem

Open up your Gemfile and add these lines

gem 'devise'
gem 'activeadmin', github: 'activeadmin'
gem 'inherited_resources', github: 'activeadmin/inherited_resources'

and run

bundle install

to install the gems.

After updating your bundle, run the installer

rails generate active_admin:install

The installer creates an initializer used for configuring defaults used by Active Admin as well as a new folder at app/admin to put all your admin configurations.

Migrate your db and start the server:

rails db:migrate
rails server

Creating your first admin account

Open up the Rails console and create your new user via the AdminUser model:

rails console

Once booted (the terminal shows irb(main):001:0>), you can run this command:

AdminUser.create(:email => 'admin@railsgirls.com', :password => 'password123', :password_confirmation => 'password123')

You should see something like:

# (0.3ms)  begin transaction
# SQL (0.4ms)  INSERT INTO "admin_users" ...
# (0.9ms)  commit transaction

You can exit the console session with a simple exit command:

irb(main):002:0> exit

Accessing your admin panel

Visit http://localhost:3000/admin and log in using your created credentials.

Voila! You’re on your brand new Active Admin dashboard.

Add “Ideas” to back-end

To register your Idea model with Active Admin, run:

rails generate active_admin:resource Idea

Refresh your admin page and you will find Ideas in the navigation.

You can replace “Idea” with whatever model you like to register another model with Active Admin.

Setting up Strong Parameters

To prevent ActiveModel::ForbiddenAttributesError in Admin::IdeasController#update exception when updating a model you have to use the permit_params method to define which attributes may be changed:

Open up your app/admin/idea.rb file and add :name, :description and :picture to permit_params:

ActiveAdmin.register Idea do
  permit_params :name, :description, :picture
end

Remove “new”, “edit” and “destroy” for users.

If you don’t want your non-admin users to update your ideas you can easy fix this by updating your route file to only allow “index” and “show”. Add only: [:show, :index] to config/route.rb:

resources :ideas, only: [:show, :index]

Don’t forget to remove now broken links from your front-end code such as: <%= link_to 'New Idea', new_idea_path %>, <%= link_to 'Edit', edit_idea_path(idea) %>, <%= link_to 'Destroy', idea, method: :delete, data: { confirm: 'Are you sure?' } %>

Voila! You can now manage your Ideas from your new admin panel!

What next?


Want to learn more? View more guides!