ruby-on-railsrubygemsthoughtbot

Customizing fields with the administrate gem for rails


I cannot seem to find any documentation on how to modify the administrate gem's default dashboards in order to customize what gets displayed in the index and show pages. Here's my specific goal:

Right now, instead, I get a not-so-useful "Author #4" as the record label. Here's the automatically generated dashboard:

class ArticleDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    author: Field::BelongsTo,
    id: Field::Number,
    title: Field::String,
    content: Field::Text,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze
  [snip]
end

The "Customizing Dashboard" documentation page says:

Each of the Field types take a different set of options, which are specified through the .with_options class method.

So I figure that calling with_options on Field::BelongsTo might be the way to go, but what options are available for that field (or for any other, for that matter)?


Solution

  • In administrate, you may customize how a resource is displayed by overwriting the #display_resource method in the resource's dashboard.

    All of your dashboards inherit from Administrate::BaseDashboard, which uses the following method to display resources:

    def display_resource(resource)
      "#{resource.class} ##{resource.id}"
    end
    

    You'll want to add something like this to the AuthorDashboard to overwrite the default:

    def display_resource(author)
      author.last_name
    end