Active Admin 3.2.0 — Intro
How To Use Active Admin 3.2 — Part XI — Effortlessly Tailor the Admin Interface to Your Needs— RailsSeries#Episode 13
Greetings!
This gem is quite intriguing as it heavily utilizes either auto-generated code or DSLs to tailor the admin interface. One notable aspect is its robust security feature: there’s no possibility for anyone to elevate their role to admin and gain unauthorized access. This is ensured by the absence of admin privileges recorded in the admin user table, which greatly enhances security.
Let’s dive into the basics together, so you can get off to a smooth start with this gem that’s widely embraced by the community.
Let’s Get Started!
0#step— Run in your Terminal and open vs code:
rails new admin_example
cd admin_example
code .
1#step — GoTo Gemfile
:
gem 'activeadmin', '~> 3.2.0'
gem 'devise'
gem 'sassc'
2#step — Run:
bundle install
3#step — Now the fun begins!
rails g active_admin:install
4#step — Run:
rails db:migrate
5#step — Run
rails g scaffold User name email password_digest password_confirmation
6#step — Run:
rails g scaffold Post title body:text published_at:datetime user:references
7#step — GoTo:
app/models/application_record.rb
def self.ransackable_attributes(_auth_object = nil)
%w[id email created_at updated_at]
end
8#step — Run the app:
rails db:migrate
rails db:seed
rails s
9#step — Let’s register our models. Run:
rails g active_admin:resource User
rails g active_admin:resource Post
10#step — Let’s customize each file created by the gem:
GoTo:
app/admin/posts.rb
ActiveAdmin.register Post do
permit_params :title, :body, :published_at, :user_id
filter :title_cont, label: 'Title' # Add a filter for the name attribute
filter :body_cont, label: 'Body' # Add a filter for the email attribute
filter :published_at, label: 'Published_at'
# filter :user_id, with: 'user_id'
index do
selectable_column
id_column
column :title
column :body
actions
end
form do |f|
f.inputs do
f.input :user
f.input :title
f.input :body
end
f.actions
end
end
11#step —GoTo:
app/admin/users.rb
ActiveAdmin.register User do
permit_params :name, :email # Ensure name and email are permitted parameters
filter :name_cont, label: 'Name' # Add a filter for the name attribute
filter :email_cont, label: 'Email' # Add a filter for the email attribute
index do
selectable_column
id_column
column :name
column :email
actions
end
form do |f|
f.inputs do
f.input :name
f.input :email
end
f.actions
end
end
12#Step — GoTo:
app/models/post.rb
belongs_to :user
def self.ransackable_attributes(_auth_object = nil)
%w[user_id title body] # Allow searching by user_id, title and body
end
def self.ransackable_associations(_auth_object = nil)
[] # We don't have any searchable associations in this case
end
13#Step — GoTo:
app/models/user.rb
has_many :posts
def self.ransackable_attributes(_auth_object = nil)
%w[name email] # Allow searching by name and email
end
def self.ransackable_associations(_auth_object = nil)
[] # We don't have any searchable associations in this case
end
14#Step — rails s
:
GoTo: http://127.0.0.1:3000/admin/
We believe that’s everything for now.
In our next episode, let’s work on upgrading our website to give it a more professional appearance.
We hope you grasp the big picture here!
Goodbye!
Credits & References
Rails Admin Interfaces with ActiveAdmin by Chris Oliver (GoRails)
Setting up Strong Parameters by github.com/activeadmin
Git Tags
git tag -a v1 -m "ActiveAdmin 3.2.0 : Go to https://medium.com/jungletronics/rails-active-admin-3-2-0-85d04f40e066" -m "0- Load the necessary gems;" -m "1- Install Active Admin;" -m "2-Generate scaffolds for Post and User Models;" -m "3- Update the application record file;" -m "4- Migrate and test default settings;" -m "5- Register the models in the framework;" -m "6-Modify the admin files and the models accordingly;You're all set!" -m "Thank you for downloading this project 😍️"
git push origin v1
Related Posts:
01# Quick Start — Part I — Get started with a very simple rails 7 frame;
02# Navbar Creation — Part II — Create a Navbar & User Functionalities in Dropdown;
03# Features — Part III — Implements some Cool advanced features.