Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Given that an Article belongs_to an Author
  • When I create an Article
  • I want to see the Author's last name in the dropdown list for the associated field
  • And once saved, I want to see the Author's last name in the Article's index and show pages

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)?

like image 637
Giuseppe Avatar asked Oct 18 '22 03:10

Giuseppe


1 Answers

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
like image 74
Ian Agne Avatar answered Oct 21 '22 01:10

Ian Agne