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

Dev*_*Dev 5 ruby-on-rails carrierwave

我在邮件中有这个:

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

在电子邮件模板中,我有以下内容:

<%= image_tag attachments['logo.png'].url %>
Run Code Online (Sandbox Code Playgroud)

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

现在这是一个很奇怪的部分,我有另一个不在资产管道中但存储在数据库中的图像,以及一个循环存储在数据库中的其他图像。而且它们都没有出现在电子邮件中。电子邮件通过没有任何错误。任何想法可能有什么问题,我该如何调试?

<%= image_tag @account.image.thumb.url %>
   <% @attachments.each do |attachment| %>
     <%= image_tag attachment.images.thumb.url %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

I'm using the gem carrierwave to attach image and I uploading this version:

version :thumb do
  process resize_to_fill: [1024, 768]
end
Run Code Online (Sandbox Code Playgroud)

nat*_*vda 5

这里有两个选项:要么您的图像存储在数据库中,还具有一个面向公众的url,然后您就可以使用它们。但是很可能它们没有这样做,然后您还必须在attachments电子邮件中添加图像,然后attachments在构建电子邮件时使用。

因此,在您的邮件中输入类似以下内容:

@attachments.each do |attachment|
  attachments.inline[attachment.image.original_filename] = attachment.image.thumb.read
end
Run Code Online (Sandbox Code Playgroud)

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

<% @attachments.each do |attachment| %>
  <%= image_tag attachments.inline[attachment.image.original_filename].url %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

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