ActiveStorage "media library" / dashboards for blobs and attachments
What were you trying to do?
I tried to generate dashboards to visualize/manage our ActiveStorage blobs and attachments, hopefully in order to see which kind of blobs we have in the DB, which records are pointing to them, what size they have, which ones are orphan, etc.
Or is there some "official" way to get such a "media library/manager" in Rails/ActiveStorage?
How do other Rails projects answer this need? Wordpress has a "Media library" showing thumbnails of all available media and it would be great to get a basic equivalent of this. Under the hood as a storage service I am currently trialling cloudinary, which does act as such a media library where all assets can be browsed, but the correspondence between the assets visible there and the contents of the DB is not obvious. For instance, how to find where a specific image found in Cloudinary is used in our app? How to identify orphan files? Sometimes I've been seeing inexplicable ActiveStorage::IntegrityError
which led me to feel like more transparency into what's happening would be useful. EDIT: I was using Cloudinary as storage service and the IntegrityError seemed to occur when I moved the images into sub-folders in the Cloudinary dashboard (to organize the assets a bit better). I didn't realize this would affect the access from ActiveStorage. Based on this comment it looks like I might have been able to re-establish the link by updating the blob's key
.
Anyway, adding active_storage_blobs
and active_storage_attachments
to Administrate looks like a useful solution to better visualize the ActiveStorage assets, which led me to write this issue.
NB: this question concerns managing all ActiveStorage assets, not how to display/edit an indiviual resource's ActiveStorage attachment. I already managed to do that using administrate-field-active_storage
.
What did you end up with (logs, or, even better, example apps are great!)?
To start, I tried to make a dashbord for ActiveStorage::Blob. As the model is namespaced, first it is necessary to fix the location of the generated controller and dashboard (see https://github.com/thoughtbot/administrate/issues/1291#issuecomment-545054789):
rails g administrate:dashboard ActiveStorage::Blob
mkdir app/controllers/admin/active_storage/ app/dashboards/active_storage/
mv app/controllers/admin/blobs_controller.rb app/controllers/admin/active_storage/
mv app/dashboards/blob_dashboard.rb app/dashboards/active_storage/
I also add the route:
# config/routes.rb
Rails.application.routes.draw do
...
namespace :admin do
...
namespace :active_storage do
resources :blobs
end
end
...
end
OK, now I can view /admin
, with "Active Storage Blobs" visible in the left menu:

However if I click it, which goes /admin/active_storage/blobs
, I get undefined method 'admin_blob_path'
thrown by polymorphic_path([namespace, resource])
and various link_to
in _collection.html.erb
. I can workaround all these issues with this kind of hack:
<% show_path = if resource.is_a? ActiveStorage::Blob
admin_active_storage_blob_path(resource)
else
[namespace, resource]
end
%>
# And similar for edit and destroy routes
What versions are you running?
- Rails 6.0.2.2
- administrate 0.13.0 revision c28e4c63
- cloudinary 1.13.2
- administrate-field-active_storage 0.2.2