Customizing Forms in the Administrate Dashboard
In this guide we'll walk through how to customize forms in the administrate admin dashboard, including customizing the forms for the AdminUser dashboard.
Guide Tasks
  • Read Tutorial

In the last lesson we walked through a demo of how to use the Administrate gem. However as you may remember we ran into a bug when it came to adding new admin users.

Thankfully Administrate was very well built and allows us to easily customize each of the dashboard pages.

In order to clean up the AdminUser interface we'll update this dashboard file, removing the items that aren't needed and adding in ones that we'll need to add new users

# app/dashboards/admin_user_dashboard.rb

require "administrate/base_dashboard"

class AdminUserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    posts: Field::HasMany,
    id: Field::Number,
    email: Field::String,
    password: Field::String,
    sign_in_count: Field::Number,
    current_sign_in_at: Field::DateTime,
    last_sign_in_at: Field::DateTime,
    current_sign_in_ip: Field::String.with_options(searchable: false),
    last_sign_in_ip: Field::String.with_options(searchable: false),
    first_name: Field::String,
    last_name: Field::String,
    avatar: Field::Text,
    username: Field::String,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
    type: Field::String,
  }.freeze

  COLLECTION_ATTRIBUTES = [
    :posts,
    :id,
    :email,
  ].freeze

  SHOW_PAGE_ATTRIBUTES = [
    :posts,
    :id,
    :email,
    :sign_in_count,
    :current_sign_in_at,
    :last_sign_in_at,
    :current_sign_in_ip,
    :last_sign_in_ip,
    :first_name,
    :last_name,
    :avatar,
    :username,
    :created_at,
    :updated_at,
    :type,
  ].freeze

  FORM_ATTRIBUTES = [
    :posts,
    :email,
    :password,
    :first_name,
    :last_name,
    :avatar,
    :username,
    :type,
  ].freeze
end

The notable changes here revolved around removing items related to the password and adding in the password attribute to the ATTRIBUTE_TYPES list and to the FORM_ATTRIBUTES. Now if we run the application you'll see that this is working properly:

large

Our new administrate@devcamp.com user can be tested in the regular application, and as you'll see they can login and properly access the application, so everything is working properly and now we can create admins via the admin dashboard.

large

For extra credit, I'd recommend going through the full set of dashboard files and clear up all of the default attributes so that only the necessary attributes are being shown on the admin pages.

What's Next?

The next guide is going to be fun and we're going to enable usernames to become subdomains in a Rails application.

Resources