简体   繁体   English

用rails_admin关联中的文本替换ID

[英]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. 对关联对象(例如has_and_belongs_to)使用rails_admin时,它将对象的ID显示为关联。 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. 您可以使用RailsAdmin DSL object_label_method更改将字段呈现给用户的方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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