首页 > 解决方案 > 如何在 RailsAdmin 中显示 ActiveStorage 对象的文件名或自定义文本

问题描述

我在 RailsAdmin 中包含了我的模型中的 ActiveStorage 属性,如下所示:

config.model 'Employee' do
      list do
        field :resume, :active_storage
      end
end

这有效,但它在列表视图中显示 ActiveStorage 对象:

在此处输入图像描述

我宁愿显示文件名或其他一些文本,同时仍然能够单击文本并下载文件。

标签: ruby-on-rails-5rails-admin

解决方案


我想通了如下:

field :resume, :active_storage do
  pretty_value do
    if value
      path = Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
      bindings[:view].content_tag(:a, value.filename, href: path)
    end
  end

推荐阅读