Administrate: Add an additional field - based on other fields value?

Created on 31 Dec 2018  路  2Comments  路  Source: thoughtbot/administrate

Based on the guide, it looks like Administrate only can customize an existing field because the Field assumes the original value will be passed through the data variable. How do I render on show page if the "new field" needs to be computed using more than its own original value?

For example, I have a place model that stores geocoded coordinates from Google. Instead of just displaying the lat & lng, I'd like to also show a static google map image on the show page. Google provides a very easy URL to generate the static image, however, this url needs both the lat & lng in order to work. I couldn't figure out how to add this image to the show page because this new field, if we gonna create a new field, needs data from two other attributes, lat & lng.

I am probably missing something very simple, hopefully. Appreciate any help in advance!

feature fields

Most helpful comment

For values not in a column, I usually just define a method in the model and reference it in the dashboard. For example:

class Place < AplicationRecord
  def google_maps_url
    "www.google.com/maps?lat=#{lat}&long=#{long}"
  end
end

class PlaceDashboard < Administrate::BaseDashboard
    ATTRIBUTE_TYPES = {
      google_maps_url: Field::String
    }
end

Something like this should work with the example you posted form the guide.

All 2 comments

For values not in a column, I usually just define a method in the model and reference it in the dashboard. For example:

class Place < AplicationRecord
  def google_maps_url
    "www.google.com/maps?lat=#{lat}&long=#{long}"
  end
end

class PlaceDashboard < Administrate::BaseDashboard
    ATTRIBUTE_TYPES = {
      google_maps_url: Field::String
    }
end

Something like this should work with the example you posted form the guide.

Another way of solving this would be to use a presenter, which were looking at in #1773 and it's related issues. I'm going to close this as there's an immediate solution too. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

conwayanderson picture conwayanderson  路  4Comments

brynmrk picture brynmrk  路  3Comments

trandoanhung1991 picture trandoanhung1991  路  3Comments

dbryand picture dbryand  路  3Comments

MatthiasRMS picture MatthiasRMS  路  3Comments