Every time I retrieve an ActiveStorage attachment URL (using object.attachment.service_url
), is a new one. Is there a way to use always the same one?
EDIT
storage.yml
file
amazon:
service: S3
access_key_id: <%= ENV['AWS_KEY'] %>
secret_access_key: <%= ENV['AWS_SECRET'] %>
region: 'sa-east-1'
bucket: 'production-bucket'
test:
service: S3
access_key_id: <%= ENV['AWS_KEY'] %>
secret_access_key: <%= ENV['AWS_SECRET'] %>
region: 'sa-east-1'
bucket: 'staging-bucket'
local:
service: Disk
root: <%= Rails.root.join("storage") %>
Maybe this line can help you
Rails.application.routes.url_helpers.rails_blob_path(Object.attachement, only_path:true)
You can add this method to your model
def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_path(self.attachement, only_path:true)
else
nil
end
end
And call it from anywhere.
If your model has many attachments
def attachment_url(item_attached)
if item_attached.attached?
Rails.application.routes.url_helpers.rails_blob_path(item_attached, only_path:true)
else
nil
end
end
Object.attachement_url(Object.image)