每次我检索ActiveStorage附件URL(使用object.attachment.service_url
)都是一个新的URL。有没有一种方法可以始终使用相同的方法?
编辑
storage.yml
文件
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") %>
答案 0 :(得分:2)
也许这条线可以帮助您
Rails.application.routes.url_helpers.rails_blob_path(Object.attachement, only_path:true)
您可以将此方法添加到模型中
def attachment_url
if self.attachment.attached?
Rails.application.routes.url_helpers.rails_blob_path(self.attachement, only_path:true)
else
nil
end
end
并从任何地方调用它。
如果您的模型有很多附件
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)