Search code examples
ruby-on-rails-5rails-admin

How to display filename or custom text for ActiveStorage object in RailsAdmin


I have included an ActiveStorage attribute from my model in RailsAdmin as follows:

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

This works but it displays the ActiveStorage object in the list view:

enter image description here

I would prefer to show the filename or some other text instead while still being able to click the text and download the file.


Solution

  • I figured it out as follows:

    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