【问题标题】:Rails 3.1: Trouble on displaying images in mailer view filesRails 3.1:在邮件视图文件中显示图像时出现问题
【发布时间】:2011-10-18 15:52:27
【问题描述】:

我正在使用 Ruby on Rails 3.1,我想将我的网站徽标(即通过新资产管道处理的图像)添加到电子邮件中。

如果在我的邮件视图文件中我声明如下:

<% # Note: '@root_url' is my application hostname (eg: http://www.mysite.com) %>
<%= link_to image_tag( "#{@root_url.to_s}/images/logo.png"), @root_url.to_s %>

它在 production 模式下不起作用(也就是说,我无法显示徽标图像),因为我认为资产管道使用指纹技术并且在收到的电子邮件中它没有.检查电子邮件中的 HTML 徽标元素,我得到如下信息:

<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting

我该如何解决这个问题?


在我的production.rb 文件中,我有以下注释掉的代码:

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 asset-pipeline


【解决方案1】:

config/environments/production.rb(以及需要的其他环境文件)中添加:

config.action_mailer.asset_host = 'http://mysite.com'

之后,rails 会自动在 image_tag 生成的路径前添加主机名

# haml
= image_tag 'foo.jpg'

会变成

#html
<img alt="" src="http://mysite.com/assets/foo.jpg" >

...同样申请image_path

#haml
%table#backgroundTable{background: image_path('email-background.jpg'), width: '100%', :border => "0", :cellpadding => "0", :cellspacing => "0"}

会变成

<table background="http://mysite.com/assets/email-background.jpg" border="0" cellpadding="0" cellspacing="0" id="backgroundTable" width="100%">

小心!!!

# this will make your emails display images
config.action_mailer.asset_host = 'http://mysite.com'

不同于

# this wont make your email images work
config.action_controller.asset_host = "http://mysite.com" 

【讨论】:

  • 有谁知道config.action_mailer.asset_host = 'website' 在 Rails 4 中是否默认启用?也就是说,如果您希望资产主机不是根域,您是否只需要指定该选项?
  • 为什么默认不启用?或者至少应该出现在这一行, config.action_mailer.asset_host ,就像 config.action_controller.asset_host 一样!不管怎样,谢谢大佬!它有效;)
【解决方案2】:

所有这些答案都假设您正在使用资产管道,但从您的示例中,您在 /public/images 中指定了一个图像 - 这不是资产管道的一部分,因此所有基于 asset_path答案将不起作用,而且您最初的指纹假设也不正确。

如果您将图像放在 /public/images 中,您希望您的图像标签具有 http://yoursite.com/images/the-image.jpegsrc,没有指纹,没有资产路径,什么都没有 - 只需将其硬编码到您的视图中:

<img src="<%=@root_url%>/images/logo.png">

但是,你必须把文件放在那个位置!如果您在 /app/assets/images 中有您的图片,那么您需要使用 image_tag 和其他人回答的资产管道。

【讨论】:

    【解决方案3】:

    另一种方法是在邮件中包含图像徽标。邮件也可以离线查看。您可以使用以下代码在 Mailer 类中添加徽标..

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

    此代码会将您的图片包含在邮件中。我相信当您想在邮件中显示附件时,您需要执行以下操作:

    Class YourMailer < ActionMailer::Base
    def sendmail
    .....
    attachments.inline['your_logo.png'] = File.read("#{Rails.root}/assets/images/your_logo.png")
    end
    

    在你的 sendmail.html.erb 视图中你可以使用 image_tag 方法:

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

    注意:如果邮件没有正确显示,您也可以尝试以下解决方案: Rails attachments inline are not shown correctly in gmail 然后您的邮件也可以正确离线查看。

    【讨论】:

      【解决方案4】:

      您是否尝试过添加类似的内容

      config.action_mailer.default_url_options = { :host => 'www.example.com' }
      

      到您的config/enviroments/production.rb 文件

      【讨论】:

        【解决方案5】:

        试试:

        <%= link_to image_tag( "#{@root_url.to_s}/assets/logo.png"), @root_url.to_s %>
        

        【讨论】:

        • 如果您的logo.png 位于您的根目录images 文件夹中,如果它位于图像内的其他文件夹中,例如system,您可以添加/assets/system/logo.png
        • 或使用asset_path,如image_tag(@root_url.to_s+asset_path('logo.png'))
        【解决方案6】:

        您正在为 image_tag 提供一个绝对 url,因此它认为它不需要做任何指纹识别或其他任何事情,除了反刍您给它的字符串。我会试试的

        link_to( image_tag('logo.png'), @root_url)
        

        您还需要设置 actioncontroller 的资产主机以获取 rails 以生成图像的完整 url 而不仅仅是路径

        需要注意的一点:如果您更改图像,那么指纹显然会发生变化,因此您之前发送的所有电子邮件中的 inage URL 将变为无效。您可能希望考虑内嵌图片,尽管这些显然会增加电子邮件的大小

        【讨论】:

          【解决方案7】:

          试试这个

          <%= link_to image_tag( asset_path, 'logo.png'), @root_url.to_s %>
          

          【讨论】:

            【解决方案8】:

            添加 mode:'rb' 对我有用:

            attachments.inline['Logo.jpg'] = File.read(File.join(Rails.root,'app','assets','images','Logo.jpg'), mode: 'rb')
            

            【讨论】:

              【解决方案9】:

              如果你编译你的资产:

              RAILS_ENV=production bundle exec rake assets:precompile
              

              并在您的视图中使用asset_path:

              <%= link_to image_tag( asset_path('logo.png') ), @root_url.to_s %>
              

              --它应该在开发和生产中都可以使用。这就是我的观点和 .css.scss.erb 样式表的方式。我认为它是一个邮寄者的视图并没有什么不同。

              【讨论】:

              • 它对我不起作用。它生成以下 HTML 代码:&lt;img src="http://assets/system/logo.png"&gt;(没有指纹和主机名)。
              【解决方案10】:

              确保您的 html 页面有以下标题

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
              

              并将图像渲染为:

              <%= image_tag('http://'+@url+'/images/header.jpg')%>
              

              如果你想链接到图像然后

              <%= link_to image_tag('http://'+@url+'/images/header.jpg'),root_path %>
              

              @url = '你的网站地址'

              【讨论】:

              • @user502052 这可能对你有帮助api.rubyonrails.org/classes/ActionMailer/Base.html
              • 我不确定你认为 doctype 声明会对像这样的简单标记做什么 - 图像已经存在了一段时间,所以它不是浏览器(或邮件客户端)呈现的问题页面不同
              • +@url+ 是非红宝石方式。不要那样做。这不是 PHP。
              猜你喜欢
              • 2011-11-06
              • 2019-11-03
              • 1970-01-01
              • 1970-01-01
              • 2018-02-13
              • 2022-12-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多