Creating an Admin Panel with ActiveAdmin in Ruby on Rails

This article will discuss how to implement an admin panel with activeadmin in Ruby on Rails. First, create a project and a few models.

1
2
3
4
rails new active_admin_blog
rails g scaffold User email name
rails g scaffold Post title body user:belongs_to
rails db:migrate

Now in app/models/user.rb, define the following relationship:

1
2
3
4
5
# app/models/user.rb

class User < ApplicationRecord
 has_many :posts
end

Add the required gems.

1
2
gem 'activeadmin'
gem 'devise'

Run the following command:

1
bundle install

Now configure active admin.

1
rails g active_admin:install

Add resources for active admin.

1
2
rails generate active_admin:resource User
rails generate active_admin:resource Post

Migrate the database.

1
rails db:migrate

Run rails server and look at http://localhost:3000/admin/login - you will see an admin login form. Open rails console and create an admin.

1
AdminUser.create(email: 'admin@email.com', password: '123123', password_confirmation: '123123')

Uncomment the following line from app/admin/users.rb:

1
2
3
# app/admin/users.rb

permit_params :email, :name

Uncomment the following line from app/admin/posts.rb:

1
2
3
# app/admin/posts.rb

permit_params :title, :body, :user_id

Once you’ve created a user, everything should work smoothly!