【问题标题】:How is the best method to add an active storage representation to a mailer?向邮件程序添加活动存储表示的最佳方法是什么?
【发布时间】:2021-04-25 22:15:11
【问题描述】:

我想在邮件中使用 active_storage 表示。

rails_blob_path(@post.photos.first.img).variant(resize: "300x300") 不起作用,通过rails_blob_path(@post.photos.first.img) 生成的所有链接将在 5 分钟后过期。

有没有办法生成永久长寿命的网址?

Rails.application.routes.url_helpers.rails_blob_url(@post.photos.first.img.variant(resize: "300x300"), only_path: true)

返回 NoMethodError:未定义的方法 `signed_id'

【问题讨论】:

    标签: ruby-on-rails rails-activestorage mailer


    【解决方案1】:

    Rails 6.1 引入了对公共存储的支持。例如:

    s3_public:
      service: S3
      access_key_id: <%= Rails.application.credentials.dig(:s3, :access_key_id) %>
      secret_access_key: <%= Rails.application.credentials.dig(:s3, :secret_access_key) %>
      bucket: bucket_name
      public: true
    

    您可以为每个附件设置存储服务,以防您不希望所有内容都公开:

    has_one_attached :pdf, service: :s3_public
    

    【讨论】:

    • 很好,但我不在 Rails 6.1 上 :(
    • @Ben 真可惜。你在哪个版本? stackoverflow.com/questions/52571555/…,这实际上不是一个解决方案。 Active Storage 是真的固执己见的,可能会一起抛弃它。
    猜你喜欢
    • 2014-09-10
    • 2023-03-15
    • 2015-03-12
    • 2012-03-21
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2013-05-23
    相关资源
    最近更新 更多