Get path to ActiveStorage file on disk
Thanks to the help of @muistooshort in the comments, after looking at the Active Storage Code, this works:
active_storage_disk_service = ActiveStorage::Service::DiskService.new(root: Rails.root.to_s + '/storage/')
active_storage_disk_service.send(:path_for, user.avatar.blob.key)
# => returns full path to the document stored locally on disk
This solution feels a bit hacky to me. I'd love to hear of other solutions. This does work for me though.
Where does active storage store files (on disk), and how can I retrieve them physically?
On your local development machine (since you mentioned local disk), you should have a file config/storage.yml
that has a block similar to below:
local:
service: Disk
root: <%= Rails.root.join('storage') %>
In this example above, I'm storing the files in a folder named storage
in the root of my rails application. Inside that folder you'll find nested folders that aren't meant to be navigated via file explorer/Finder (but you can).
Thru your rails app, via views for example, you'd use the url helpers which aren't well documented yet.
From a rails console, you can try, for a model named Foo
, with a has_one_attached :photo
Foo.last.photo.blob.key
It should give you a ~24 character string.
- The first 2 characters are a subfolder inside the folder I pointed you to above
- The next 2 characters are a subfolder inside that
Inside the subfolder is a file with the name that matches the key you printed out above (no extension). That's your file.
Rails Active Storage: Get relative disk service path for attachment
You need to use the routes helper to build of a URL to your Rails app.
https://guides.rubyonrails.org/active_storage_overview.html#linking-to-files
class Product < ApplicationRecord
attr_accessor :image_url
has_one_attached :image
def as_json(options)
h = super(options)
if self.image.attached?
h[:image_url] = Rails.application.routes.url_helpers.rails_blob_path(self.image)
end
h
end
end
How to get url of Active Storage image
For my User
which has_one_attached :avatar
I can get the url in my views with <%= image_tag url_for(user.avatar) %>
.
So, in controller I would use just url_for(user.avatar)
For Category
which has_one_attached :image
:
url_for(category.image)
Related Topics
Skip Over Iteration in Enumerable#Collect
Ruby 2.0 How to Uninclude a Module Out from a Module After Including It
Rails 3.1 with Postgresql: Group by Must Be Used in an Aggregate Function
Getting Viewable Text Words via Nokogiri
Devise Custom Messages When Validation Fails
Ruby Loaderror: Cannot Load Such File
Ror + Unable to Install Tiny_Tds
Has_Many While Respecting Build Strategy in Factory_Girl
Creating a Capistrano Task That Performs Different Tasks Based on Role
Ruby To_JSON Issue with Error "Illegal/Malformed Utf-8"
Rails How to Update a Column After Saving
Using a Class Object in Case Statement
Custom Ruby Gem in Gemfile on Heroku
Render an Erb Template with Values from a Hash
Error While Trying to Load the Gem 'Devise. Activesupport: Duration Can't Be Coerced into Integer