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
and run
to install the gems.
After updating your bundle, run the installer
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:
Creating your first admin account
Open up the Rails console and create your new user via the AdminUser
model:
Once booted (the terminal shows irb(main):001:0>
), you can run this command:
You should see something like:
You can exit the console session with a simple exit
command:
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:
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
:
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
:
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?
- Add another resource to admin such as Blog and Comments
Want to learn more? View more guides!