【问题标题】:Paperclip files not being attached to actionmailer. No such file or directory回形针文件未附加到 actionmailer。无此文件或目录
【发布时间】:2014-06-04 05:54:49
【问题描述】:

我正在尝试将回形针头像图像附加到电子邮件中,但是它似乎没有意识到这是我要附加的图像,但是当我将 URL 放在浏览器中时,它会找到该图像。我不确定是不是因为回形针末尾的随机数。

在我的用户邮件中

attachments.inline['avatar.jpg'] = File.read("localhost:3000#{@user.avatar.url(:medium)}")

在我的电子邮件中

<%= image_tag attachments['avatar.jpg'].url %>

它吐出来的url是(图片为测试用的随机库存照片)

localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965

【问题讨论】:

  • 您的模板语句&lt;%= image_tag attachments['avatar.jpg'].url -%&gt; 中是否有错字。在结束 %> 语句中有一个额外的破折号。
  • 是的,对不起。不在我的代码中。我应该复制和粘贴。我一定是不小心撞到了。

标签: ruby-on-rails ruby paperclip actionmailer


【解决方案1】:

您可以使用资产管道附加实际文件路径。没有测试过。

"#{Rails.root}/#{&lt;YourAppName&gt;::Application.assets.find_asset('avatar.jpg').pathname}"

You can find the answer here

【讨论】:

  • 这个问题是我需要知道实际的文件名。我不知道上传头像的人的文件名。我只是将附件保存为avatar.jpg。找不到文件avatar.jpg
  • 虽然paperclip 重命名了文件?或者至少保留它的路径/位置?可能是错的。
  • 没有它吐出的那个网址localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965
  • 这个user.avatar.path怎么样? source
【解决方案2】:

这是我的模型的样子

class Petition < ApplicationRecord
  has_attached_file :contract
end

在邮件中,我像这样附加了这个合同文件。希望这会有所帮助。

def send_invitation(petition)
  attachments[petition.contract_file_name] = File.read(petition.contract.path)
  mail(
    to:       @petition.email,
    subject: t('mailer.send_invitation')) do |format|
    format.html { render 'send_invitation' }
end

【讨论】:

    【解决方案3】:

    我认为你应该使用

    attachments.inline['avatar.jpg'] = @user.avatar.data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2020-08-07
      • 2021-12-26
      • 2013-01-26
      • 2021-03-28
      相关资源
      最近更新 更多