我似乎找不到任何关于如何修改 administrate gem 的文档。的默认仪表板,以便自定义在索引和显示页面中显示的内容。这是我的具体目标:
Article
belongs_to
Author
现在,相反,我得到了一个不太有用的“作者#4”作为唱片公司。这是自动生成的仪表板:
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
“Customizing Dashboard ”文档页面说:
Each of the Field types take a different set of options, which are specified through the
.with_options
class method.
所以我想那个叫
with_options
在 Field::BelongsTo
可能是要走的路,但是该领域(或任何其他领域)有哪些可用的选项?
最佳答案
在管理中,您可以通过覆盖资源仪表板中的 #display_resource 方法来自定义资源的显示方式。
您的所有仪表板都继承自 Administrate::BaseDashboard,它使用以下方法来显示资源:
def display_resource(resource)
"#{resource.class} ##{resource.id}"
end
您需要将这样的内容添加到 AuthorDashboard 以覆盖默认值:
def display_resource(author)
author.last_name
end
关于ruby-on-rails - 使用 rails 的管理 gem 自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052135/