【问题标题】:Cannot display image inside email in rails无法在 Rails 中的电子邮件内显示图像
【发布时间】:2016-08-15 10:50:02
【问题描述】:

我正在尝试在我的 rails 应用程序的电子邮件中显示我的徽标。我的图像位于 assets/images 和 public 文件夹中。 但是,该应用程序似乎从未找到我的图像。如何在电子邮件中包含我的图像? 这是我的布局 mailer.html.erb,其中图像包含在 image_tag 中

<html>
  <body>
  <div style= "text-align: center;">
    <%= image_tag "quickbed_logo.png", style: "width: 200px;"%>
  </div>
    <%= yield %>
  </body>
</html>

【问题讨论】:

  • 邮件中生成的 URL 是什么?
  • 好吧,我认为它不起作用,因为生成的 url 是相对的(它需要 localhost:3333

标签: ruby-on-rails image mailer


【解决方案1】:

你设置asset_host了吗?

development.rb

  config.action_mailer.asset_host = "http://localhost:3000"
  config.action_mailer.default_url_options = {host: "http://localhost:3000"}
  config.action_controller.asset_host = "http://localhost:3000"

production.rb

   config.action_mailer.asset_host = "https://example.com"
   config.action_mailer.default_url_options = {host: "http://example.com"}
   config.action_controller.asset_host = "http://example.com"

如果这仍然不起作用。代码中可以尝试使用image_url

<%= image_tag image_url "quickbed_logo.png", style: "width: 200px;"%>

另外,您是否启用了config.serve_static_assets

【讨论】:

  • 我确实设置了您提到的所有内容,但仍然无法正常工作
  • 这是一个配置问题。 host 属性没有被助手拾取。我已更新我的答案以包含更多配置更改。
猜你喜欢
  • 1970-01-01
  • 2014-12-05
  • 2015-10-12
  • 2018-09-01
  • 2020-08-20
  • 2013-04-21
  • 2012-06-23
  • 2011-10-17
  • 2011-11-06
相关资源
最近更新 更多