ActiveStorage service_url && rails_blob_path cannot generate full url when not using S3
Active Storage’s disk service expects to find a host for URL generation in ActiveStorage::Current.host
.
When you call ActiveStorage::Blob#service_url
manually, ensure ActiveStorage::Current.host
is set. If you call it from a controller, you can subclass ActiveStorage::BaseController
. If that’s not an option, set ActiveStorage::Current.host
in a before_action
hook:
class Items::FilesController < ApplicationController
before_action do
ActiveStorage::Current.host = request.base_url
end
end
Outside of a controller, use ActiveStorage::Current.set
to provide a host:
ActiveStorage::Current.set(host: "https://www.example.com") do
item.file_attachments.first.service_url
end