我正在使用 Ruby on Rails 3.1,我想将我的网站 Logo (即通过新 Assets 管道处理的图像)添加到电子邮件中。
如果在我的邮件 View 文件中声明如下:
<% # 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 模式下不起作用(也就是说,我无法显示 Logo 图像),因为我认为 Assets 管道使用指纹识别技术并且在收到的电子邮件中它没有.检查电子邮件中的 HTML Logo 元素,我得到如下信息:
<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"
最佳答案
在 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"
关于ruby-on-rails - rails 3.1 : Trouble on displaying images in mailer view files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7810103/