Activeadmin Gem Install and Usage

2–3 minutes

read

Activeadmin Gem Install and Usage

Steps to be followed:

  1. Install the Activeadmin gem. Put these in gemfile.
gem 'activeadmin'
gem 'cancan' # or cancancan
gem 'draper'
gem 'pundit'

2. Setting up Activeadmin

After installing the gem, you need to run the generator.

rails g active_admin:install

The generator adds these core files, among others:

  • app/admin/dashboard.rb
  • app/assets/javascripts/active_admin.js
  • app/assets/stylesheets/active_admin.scss
  • config/initializers/active_admin.rb

3. Now, migrate and seed your database before starting the server:

rake db:migrate
rake db:seed
rails server

Note: Seed is important here.

4. now check if it is working, in browser:

http://localhost:3000/admin

5. Now you can see dashboard, now to add extra tab like in my case it is of question.

run command in terminal:

$rails g active_admin:resource question

This will create a tab question in active admin page and a file is created in app/admin/questions.rb

6. Now check the tab it already displays all for modifying in that file :

index do 
    column :id 
    column :content 
    column :user 
    column "Created at", :created_at 
    column :category    
    column :status    
    actions
end

actions for view/edit/destroy

7.  Adding scope i.e. a easy way to access the unsolved questions or unreleased products etc.

add in app/admin/questions.rb

scope :unsolved

on top of index do

Now, in  model file question.rb

scope :unsolved, -> {
 where(:status => 0)
}

and in case of multiple values like posts then

scope :solvers, -> { where(:post => [0,2,3,4])}

now run server and see a tab for easy access.

8. decorate the dashboard

ActiveAdmin.register_page "Dashboard" do

menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }

content title: proc{ I18n.t("active_admin.dashboard") } do


section "Recent Questions" do
 table_for Question.order("created_at desc").limit(5) do
 column :id
 column :content
 column :user
 column "Created at", :created_at
 column :category
 column :status

end
 strong{link_to "View all Questions", admin_questions_path}

section "New Users" do
 table_for User.order("created_at desc").limit(5) do
 column :id
 column :email
 column :post
 column "Created at", :created_at
 column :last_sign_in_at


end
 strong{link_to "View all Users", admin_users_path}
end

Now to remove the css styling overriding causing due to activeadmin.scss

Just move the activeadmin.scss from app/assets/stylesheet/activeadmin.scss to vendor/stylesheet/

then it will not override.

For updating and edit to work in the admin page in dashboard, you have to permit the params.

in admin/user.rb

 permit_params :id, :email, :post, :last_sign_in_at

in admin/question.rb

permit_params :id, :content, :user, :category ,:status

add all those attributes that you want to get updated.

REFER LINK:

https://activeadmin.info/0-installation.html

Leave a comment