Best Admin predefined featured in ruby on rails

Total Blog Views: 106

Blog Status: publish

Created By: swazahmad Created at: 08-20-2021

Tags: ruby on rails rails admin admin gem rails admin configuration

In Below Article We will learn How To configure rails_admin Gem In ruby on rails In easy steps.

 

First Of all We will create New Rails Application and configure DB

 

 rails new admin-app-ruby-on-rails

Add Below Gem In GemFile

gem 'devise', '~> 4.7', '>= 4.7.2'
gem 'rails_admin', '~> 2.0'
gem 'kaminari'
Then We Will run Below Command And Install devise Gem
bundle install
rails generate devise:install
rails generate devise User

Run Below Command

rails g rails_admin:install
rails g controller Home index
rails g scaffold Student name:string address:text

Update routes.rb file

root 'home#index'

The above 2 commands will updates and routes and create a Migration File

Then run Below Command

rake db:migrate
rails s

 

Now You all set Just Try To visit

http://localhost:3000/admin

Now You will be able to see Student and user Model In admin section Without writing any single Line

You can do CRUD for all Model Present in Your rails Application

In above Picture You are able To see All Models Of My application

Now we learn How to add authentication In rails admin Gem

How To add admin Authentication in rails_admin Gem

You need to follow below Steps:

 

rails generate migration add_admin_to_users admin:boolean
rake db:migrate
Goto Rails Console, Run below Command
rails c
User.create(email: '[email protected]', password: 123456, admin: true)

From Above steps The admin user will be created.

Now Update Below File:

../config/intializers/rails_admin.rb
RailsAdmin.config do |config|
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
  config.current_user_method(&:current_user)
  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app
  end
end

Update The routes.rb file

authenticate :user, lambda { |u| u.admin == true } do
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
end

Now You are all set now Start the App and enjoy using only admin users.

Some Bonus below

How to include some model Specifically in rails_admin Gem?

update the file config/initializers/rails_admin.rb
config.included_models = [ User ]

How to include some model Specifically in rails_admin Gem?

update the file config/initializers/rails_admin.rb
config.excluded_models = [ Student ]

Thanks For reading, Feel Free to ask any Question


swazahmad

Swaz is Practice Lead of ROR Developer. he's a Blogger, Mentor, and Rubyst. He has expertise in Javascript also . he's highly skilled in designing, developing and testing web applications. He enjoys being challenged and interesting with projects that need him to



Comments



  • vishnu | almost 4 years ago
    Bravo!
  • asad | almost 4 years ago
    Awsm information
    • swazahmad | almost 4 years ago
      Thanks asad
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads