【问题标题】:Attach a pdf in asset pipeline using ActionMailer Rails 4使用 ActionMailer Rails 4 在资产管道中附加 pdf
【发布时间】:2014-09-16 05:46:12
【问题描述】:

我正在尝试将文件附加到电子邮件中。文件在assets/downloads/product.pdf

在邮件中,我有:

attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_path("product.pdf"))

我试过了:

attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_url("product.pdf"))

...甚至:

attachments["product.pdf"] = File.read(ActionController::Base.helpers.compute_asset_host("product.pdf") + ActionController::Base.helpers.compute_asset_path("product.pdf"))

我总是遇到同样的错误:

EmailJob crashed!
Errno::ENOENT: No such file or directory - //localhost:3000/assets/product.pdf

...或主题的变体。但即使我尝试在视图中使用asset_url 或者只是将url 放在浏览器中,它也可以工作:

http://localhost:3000/assets/product.pdf

我也试过直接使用:

File.read("app/assets/downloads/product.pdf")
File.read("downloads/product.pdf")

...在开发环境中有效,但在登台服务器(heroku)上无效。错误依旧:

Errno::ENOENT: No such file or directory - downloads/product-market-fit-storyboard.pdf

也试过了:

File.read("/downloads/product.pdf")
File.read("http://lvh.me:3000/assets/product.pdf")

...根本不工作。

想法?

【问题讨论】:

  • 在附加文件时尝试在http协议前添加前缀
  • 不行。还尝试使用File.read("http://lvh.me:3000/assets/product.pdf")

标签: ruby-on-rails ruby-on-rails-4 asset-pipeline actionmailer


【解决方案1】:

在使用邮件程序时,您不应该使用资产管道。如果您想在电子邮件中包含指向文件的链接,资产管道将非常有用。呈现电子邮件时,action mailer 可以访问 app 目录中的文件。

请阅读action mailer guide 中的附件。如您所见,您只需将路径传递给文件,而不是 url:

attachments['filename.jpg'] = File.read('/path/to/filename.jpg')

【讨论】:

  • 好的,现在我尝试了File.read("app/assets/downloads/product.pdf"),它适用于开发环境,但不适用于登台服务器(heroku)。我现在也会尝试将它放在其他地方。
  • 现在我尝试了File.read("downloads/product.pdf"),它在开发中有效,但在暂存时无效。 File.read("/downloads/product.pdf") 在 dev 上不起作用。
  • 除了资产有时需要被邮件程序和控制器使用,并且资产在生产中被散列。这并不能解决真正的根本问题
【解决方案2】:

你应该使用这样的语法。它对我有用,可能对你也有用。

File.open(Dir.glob("#{Rails.root}/app/assets/downloads/product.pdf"), "r")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多