How to Customize Administrate to Show Enums
Now that we have our enum functionality in place, we are going to implement the ability to show the enum values in the Administrate dashboard.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that we have our enum functionality in place, we are going to implement the ability to show the enum values in the Administrate dashboard.

Start by going to the post_dashboard.rb configuration file, and add the status field to the ATTRIBUTE_TYPES list. Also, swap out status instead of id in the COLLECTION_ATTRIBUTES list. Also, add the new status field to SHOW_PAGE_ATTRIBUTES. We won't use the status attribute in our FORM_ATTRIBUTES list since Administrate would only render the value as a string representation of the value. The full code file should look like this:

# app/dashboards/post_dashboard.rb

require "administrate/base_dashboard"

class PostDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    user: Field::BelongsTo.with_options(searchable: false),
    id: Field::Number.with_options(searchable: false),
    date: Field::DateTime.with_options(searchable: false),
    rationale: Field::Text.with_options(searchable: true),
    created_at: Field::DateTime.with_options(searchable: false),
    updated_at: Field::DateTime.with_options(searchable: false),
    status: Field::Text.with_options(searchable: true),
  }.freeze

  COLLECTION_ATTRIBUTES = [
    :user,
    :status,
    :date,
    :rationale,
  ].freeze

  SHOW_PAGE_ATTRIBUTES = [
    :user,
    :status,
    :id,
    :date,
    :rationale,
    :created_at,
    :updated_at,
  ].freeze

  FORM_ATTRIBUTES = [
    :user,
    :date,
    :rationale,
  ].freeze

end

Next, go to the seeds.rb file and create an admin user.

# db/seeds.rb

AdminUser.create(email: "admin@test.com", password: "asdfasdf", password_confirmation: "asdfasdf", first_name: "Admin", last_name: "Name")

puts "1 Admin User created"

Next, let's wipe out our database with the command:

bundle exec rake db:setup

This will load the database with our clean data and new AdminUser.

If you open the browser and log in with the new credentials, you can see the posts and users page. You can also see the status field.

large

Resources