简体   繁体   中英

Replace ID with text in rails_admin associations

when using rails_admin for associated objects (like has_and_belongs_to) it shows the ID of the object as the association. This isn't a great deal for the users so I'ld like to change this for showing the text of the associated object.

Is this solvable?

Here a little example:

First Model:

class Menu
  include Mongoid::Document

  field :date, type: Date

  has_and_belongs_to_many :meal
end

Second Model:

class Meal
  include Mongoid::Document

  field :text, type: String

  has_and_belongs_to_many :menu
end

So it shows something like this:

因此显示如下:

But I'ld love to see the Text of the meals instead.

Simply define a title-method do the trick:

def title
  self.text
end

You could use the RailsAdmin DSL object_label_method to change how the field is presented to the user.

In your case, something like this might do the trick:

RailsAdmin.config do |config|
  config.model Menu do
    list do
      field :meal do
        pretty_value do
          value.text
        end
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM