Ruby, ActiveAdmin10 August, 2024

How to Set Up Active Admin in Rails 7: A Complete Installation Tutorial

Infosia

Nilesh Patil

Founder

ActiveAdmin Using Ruby on Rails

ActiveAdmin is a Ruby on Rails plugin that streamlines the creation of admin dashboards for managing application data. It automatically creates a CRUD interface for models, allowing administrators to create, read, update, and remove records. It supports customisable views, filters, batch operations, and scopes.

Prerequisites

  • Ruby Version: 3.2.4
  • Rails Version: 7.2.1
  • Working Rails App

Devise Gem Inclusion

If you have already added gem "devise", "~> 4.9" into the Gemfile, then you can skip this section.

# Gemfile

gem "devise", "~> 4.9"

Devise Setup

Run the following command to create a controller named Home (you may name it whatever you want):

> rails generate controller Home index

This will create:

  • A HomeController with an index action.
  • A view file for the homepage (app/views/home/index.html.erb).
  • The necessary routes for the controller action.

Set Up the Route for the Homepage

Next, you must configure the root route to the homepage. Open the config/routes.rb file and set the root path as follows

# config/routes.rb
Rails.application.routes.draw do
  root 'home#index' # Sets the root path
  # other routes can be added here
end

ActiveAdmin Installation

We need to add a couple gems to the Gemfile.

# Gemfile

gem 'activeadmin'
gem 'sass-rails', "~> 6.0"

Install the Gems in the terminal using bundle install.

bundle install

Generate an Active Admin Model to authenticate and authorise the user.

rails generate active_admin:install User
      invoke  devise
    generate    No need to install devise, already done.
      invoke    active_record
      create      db/migrate/20241004092949_devise_create_users.rb
      create      app/models/user.rb
      invoke      test_unit
      create        test/models/user_test.rb
      create        test/fixtures/users.yml
      insert      app/models/user.rb
       route    devise_for :users
        gsub    app/models/user.rb
        gsub    config/routes.rb
      append    db/seeds.rb
      create  config/initializers/active_admin.rb
      create  app/admin
      create  app/admin/dashboard.rb
      create  app/admin/users.rb
      insert  config/routes.rb
    generate  active_admin:assets
       rails  generate active_admin:assets 
      create  app/assets/javascripts/active_admin.js
      create  app/assets/stylesheets/active_admin.scss
      create  db/migrate/20241004092951_create_active_admin_comments.rb

Add ransackable methods into Active Record

 # application_record.rb
class ApplicationRecord < ActiveRecord::Base
 primary_abstract_class

 def self.ransackable_attributes(_auth_object = nil)
   column_names + _ransackers.keys
 end

 def self.ransackable_associations(_auth_object = nil)
   reflect_on_all_associations.map { |a| a.name.to_s } + _ransackers.keys
 end
end

Start the Server

> rails db:migrate db:seed
> rails server

Visit http://localhost:3000/admin and log in as the default user:

User: admin@example.com
Password: password

Execute the migration

> rake db:migrate

Generate ActiveAdmin Resource with below command.

> rails generate active_admin:resource post title:string body:text published:boolean

Enable the permit params in app/admin/posts.rb

permit_params :title, :body, :published

Active Admin Views 

Post List Post Form

Need help on your Ruby on Rails, React or
Custom Development project?

Book A Call
Infosia

Helping you maximize operations management with digitization

Office

B-401, Sector-4, Moshi, Pune

Contacts

(+91) 810 800 4135

Hello@infosiaTech.com

© 2024. All rights reserved

InfosiaInfosiaInfosiaInfosia