Rails 5:图像和图像循环未在电子邮件中显示

Rails 5: image and loop of images are not showing in email

我在邮件中有这个:

attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")

在电子邮件模板中我有这个:

<%= image_tag attachments['logo.png'].url %>

它工作正常,徽标确实出现在电子邮件中。

现在这是奇怪的部分,我有另一个图像不在资产管道中但存储在数据库中,还有一个循环存储在数据库中的其他图像。其中 none 出现在电子邮件中。电子邮件顺利通过。知道可能出什么问题以及如何调试它吗?

<%= image_tag @account.image.thumb.url %>
   <% @attachments.each do |attachment| %>
     <%= image_tag attachment.images.thumb.url %>
<% end %>

我正在使用 gem carrierwave 附加图片,我上传的是这个版本:

version :thumb do
  process resize_to_fill: [1024, 768]
end

这里有两个选项:存储在数据库中的图像也有面向 public 的 url,然后您可以使用它们。但很可能他们没有,然后你必须将图像作为 attachments 添加到你的电子邮件中,然后在构建电子邮件时使用 attachments

所以在你的邮件中写这样的东西:

@attachments.each do |attachment|
  attachments.inline[attachment.image.original_filename] = attachment.image.thumb.read
end

然后在您的邮件视图中,您可以再次遍历所有 @attachments,并使用正确的 attachments.inline[...],例如

<% @attachments.each do |attachment| %>
  <%= image_tag attachments.inline[attachment.image.original_filename].url %>
<% end %>

注意:我不完全确定这里的语法是否正确(你使用 images 但我认为它应该是单数?你还应该检查什么是你的图像最好的独特方式,也许 original_filename 不理想,您也可以只使用 attachment.

id