我怎样才能不不断更改 Active Storage 附件 URL?

How can I don't keep changing Active Storage attachments URLs?

每次我检索 ActiveStorage 附件 URL(使用 object.attachment.service_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") %>

也许这条线可以帮到你

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)