Adding Custom Field Types

If your application deals with a nonstandard data type, you can create an Administrate::Field object to help display the custom data type across the dashboard.

Administrate::Field objects consist of two parts: a Ruby class and associated views.

For example, let's create a Field that displays Gravatars based on an email.

First, we'll run a generator to set us up with the files we need:

rails generate administrate:field gravatar

This creates a few files:

We can edit the app/fields/gravatar_field.rb to add some custom logic:

# app/fields/gravatar_field.rb
require 'digest/md5'

class GravatarField < Administrate::Field::Base
  def gravatar_url
    email_address = data.downcase
    hash = Digest::MD5.hexdigest(email_address)

Next, we can customize the partials to display data how we'd like. Open up the app/views/fields/gravatar_field/_show.html.erb partial. By default, it looks like:

<%= %>

Since we want to display an image, we can change it to:

<%= image_tag field.gravatar_url %>

You can customize the other generated partials in the same way for custom behavior on the index and form pages.