Total Blog Views: 52
Blog Status: publish
Created By: swaz_ahmed Created at: 06-25-2024
Tags: ror rails admin ActiveAdmin Ruby on Rails Admin Dashboard ActiveAdmin Customization Rails Plugins ActiveAdmin Filters Resource Management ActiveAdmin Configuration
What is ActiveAdmin?
ActiveAdmin is a Ruby on Rails framework that simplifies the creation of elegant backends for your applications. It comes with a powerful DSL (Domain Specific Language) that allows you to scaffold and modify your Rails backend quickly. This feature is incredibly useful for support teams, enabling them to manage data independently without needing constant developer intervention.
Setting Up ActiveAdmin
First, update your Gemfile to include the necessary gems for integrating ActiveAdmin with Rails 7. We will use the master branch of ActiveAdmin from GitHub due to upcoming changes, but you can revert to a stable version once it's released.
# ActiveAdmin setup gem 'activeadmin', github: 'activeadmin/activeadmin', branch: 'master' # FIXME: revert to stable # Required for ActiveAdmin gem 'sass-rails' # Additional integrations gem 'devise' gem 'cancancan' gem 'draper' gem 'pundit'
Installing the Gems:
bundle install
Generating ActiveAdmin Files
Next, generate the necessary ActiveAdmin files and configurations:
rails generate active_admin:install
This command will create several files and directories, including an admin user model for authentication and an initializer for ActiveAdmin settings.
Running Migrations
Apply the changes to your database by running:
rails db:migrate
Starting the Rails Server
Start your Rails server to access the ActiveAdmin interface:
rails server
Accessing the Admin Interface
Open your web browser and navigate to /admin. You should see the ActiveAdmin login page. Use the default admin credentials set up during installation to log in.
Creating and Managing a Custom Model: Flag
Let’s create a custom model named Flag and manage it within ActiveAdmin. First, generate the model:
rails generate model Flag name:string status:boolean
Next, let's update the seeds to add some example flags for us to muck around with:
AdminUser.create!(email: '[email protected]', password: 'password', password_confirmation: 'password') if Rails.env.development? Flag.create!([ { "name": "feature_a", "is_active": true }, { "name": "feature_b", "is_active": true }, { "name": "feature_c", "is_active": true } ])
rails db:reset rails db:migrate db:seed
bin/rails generate active_admin:resource Flag
This will output a new resource file in:app/admin/flag.rb.
In order to be able to interact and update values, we need to uncomment what an admin is allowed to interact with in the admin panel:
ActiveAdmin.register Flag do # See permitted parameters documentation: # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters # # Uncomment all parameters which should be permitted for assignment # permit_params :name, :is_active # # or # # permit_params do # permitted = [:name, :is_active] # permitted << :other if params[:action] == 'create' && current_user.admin? # permitted # end end
Using the Dashboard
With the server running, navigate back to /admin. You should now see a section for managing Flags. Here, you can add new Flags, edit existing ones, and filter them based on their attributes.
Conclusion
ActiveAdmin significantly streamlines the process of building internal dashboards for Rails applications. By following this guide, you can quickly set up ActiveAdmin with Rails 7 and manage a custom model, enhancing the efficiency of your development workflow and empowering your support teams.
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.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link