DEV Community

Mike Rogers ✈️
Mike Rogers ✈️

Posted on • Originally published at on

Admin Panels In Rails 6 (Video Series)

One of my most reached for gems when working with Ruby on Rails is ActiveAdmin. It's a framework for building administration sections on top of your Ruby on Rails apps.

What really has made me enjoy using ActiveAdmin, is that it quietly encourages following the Rails conventions like ending a datetime field with _at. It even introduces concepts like Decorators & Pundit in a "If you do it right, it'll be super quick to build" experience.

I'd even argue, if you are building a back office Rails App it could be worthwhile to build the entire thing within ActiveAdmin & forget about writing anything remotely custom.

Videos & Source Code

Set up ActiveAdmin

Setting ActiveAdmin is pretty well documented on their repo (And might have changed since I wrote this). The only prerequisite I don't expect to change is requiring Devise be setup to handle authorisation.

In the video, I ran the commands:

$ bundle add devise
$ rails generate devise:install
$ bundle add activeadmin
$ rails generate active_admin:install --use_webpacker
Enter fullscreen mode Exit fullscreen mode

This setup Devise, created an AdminUser model, created an app/admin folder & created the files required to use Webpacker for the ActiveAdmin assets.

Resources, Filters & Scopes

ActiveAdmin comes with a few generators you can call via the terminal to help setup scaffolding. In the video I ran:

$ rails generate active_admin:resource Author
$ rails generate active_admin:resource Post
$ rails generate active_admin:resource Category
Enter fullscreen mode Exit fullscreen mode

This generated some simple scaffold for the Author, Post & Category models in our admin panel.

I then went onto customising the app/admin/posts.rb file, allowing us more fine grain control over how our users would access & modify the Posts in the app.

# app/admin/posts.rb
ActiveAdmin.register Post do
  filter :title
  filter :author_name, as: :string
  filter :created_at
  filter :categories

  scope :published

  index do
    column :id
    column :title
    column :published?
    column :author
    column :created_at

  permit_params :title, :body, :author_id, category_ids: []

  form do |f|
    f.inputs :title, :body, :author
    f.inputs "Categories" do
      f.input :categories, as: :check_boxes

  show do
    attributes_table do
      row :title
      row :body
      row :author
      row :created_at
      row :updated_at
      row :published_at
      row :categories
Enter fullscreen mode Exit fullscreen mode

Custom Actions & Decorators

In the final video, I ran through how to setup a custom action within an ActiveAdmin resource & how to use Draper to decorate our models.

# app/admin/posts.rb
ActiveAdmin.register Post do
  includes :author
  decorate_with PostDecorator

  # [snip]

  member_action :publish, method: :put do
    redirect_to resource_path, notice: "Published!"

  action_item :publish, only: :show, if: proc { !resource.published? } do
    link_to 'Publish', [:publish, :admin, resource], method: :put

  # [snip]
Enter fullscreen mode Exit fullscreen mode

I'm a big fan of using Decorators (Sometimes called presenters), they offer a tidy way to say "In my views, I want the data from this model to always be formatted like this".

So for example, if I'm always going to want to output a Posts body with a dash of formatting, I'd use a decorator to decide how I want to format that field in a single location.

# app/decorators/post_decorator.rb
class PostDecorator < ApplicationDecorator

  def body
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

voltanbro profile image

Nice, thx)