Rails 3.1:在邮件视图文件中显示图像时出现问题
我正在使用 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 %>
它在生产模式下不起作用(即,我无法显示徽标图像),因为我认为资产管道使用指纹技术并且在收到电子邮件却没有。检查电子邮件中的 HTML 徽标元素,我得到如下内容:
<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting
如何解决问题?
在我的 development.rb
文件中,我有以下注释掉的代码:
# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
I am using Ruby on Rails 3.1 and I would like to add my web site logo (that is, an image handled through the new Asset Pipeline) to an e-mail.
If in my mailer view file I state the following:
<% # 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 %>
it doesn't work in production mode (that is, I cannot display the logo image) because I think the Asset Pipeline uses the Fingerprinting technique and in the received e-mail it doesn't. Inspecting the HTML logo element in the e-mail I get something like this:
<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting
How can I solve the problem?
In my production.rb
file I have the following commented out code:
# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在 config/environments/production.rb (以及其他需要的环境文件)中添加:
之后rails会自动在
image_tag
生成的路径前面添加主机名将变成
...同样申请
image_path
会变成
watch out!!!
与
in
config/environments/production.rb
(and other enviroment files needed) add:after that rails will automatically add hostname in front of paths generated by
image_tag
will become
...same apply for
image_path
will become
watch out!!!
is different than
所有这些答案都假设您正在使用资产管道,但从您的示例来看,您在 /public/images 中指定了一个图像 - 这不是资产管道的一部分,因此所有
asset_path
基于的答案不起作用,而且您最初的指纹假设是不正确的。如果您将图像放入 /public/images 中,您希望图像标记的
src
为http://yoursite.com/images/the-image.jpeg
,没有指纹,没有资产路径,什么都没有 - 只需将其硬编码到您的视图中即可:但是,您实际上必须将文件放在该位置!如果您的图像位于 /app/assets/images 中,那么您需要使用 image_tag 和资产管道,正如其他人已经回答的那样。
All of these answers are assuming you're using the asset pipeline, but from your example, you're specifying an image in /public/images - this is not part of the asset pipeline, so all the
asset_path
based answers won't work, and further your initial fingerprinting supposition is incorrect.If you put an image in /public/images, you want your image tag to have a
src
ofhttp://yoursite.com/images/the-image.jpeg
, no fingerprint, no asset path, nothing - just hard-code it into your view:But, you have to actually have the file in that location! If you have your image in /app/assets/images, then you'll need to use image_tag and the asset pipeline as others have answered.
另一种方法是在邮件中包含图像徽标。邮件也可以离线查看。您可以使用以下代码在 Mailer 类中添加徽标。
此代码会将您的图像包含到邮件中。我相信当您想在邮件中显示附件时,您需要执行以下操作:
在 sendmail.html.erb 视图中,您可以使用 image_tag 方法:
注意:如果邮件没有收到显示正确,您也可以尝试以下解决方案:
内联 Rail 附件在 Gmail 中未正确显示
您的邮件也可以在离线状态下正确查看。
An alternative is to include the image logo in the mail. The mail could also be viewed offline. You can add the logo in you Mailer class, with the following code..
This code will include your image to the mail. I believe when you want to show your attachment in the mail you need to do the following:
And in your sendmail.html.erb view you can use the image_tag method:
note: if the mail does not get shown correctly you can alternatively try the solution at:
Rails attachments inline are not shown correctly in gmail
Your mail can then also be viewed offline correctly.
您是否尝试过将类似的内容添加
到您的 config/enviroments/development.rb 文件中
Have you tried adding something like this
to your
config/enviroments/production.rb
file尝试:
Try:
您为 image_tag 提供了一个绝对网址,因此它认为除了重新输入您提供的字符串之外不需要进行任何指纹识别或其他任何操作。我会尝试
你还需要设置actioncontroller的资产主机来让rails生成图像的完整url,而不仅仅是路径
需要注意的一个警告:如果你更改图像,那么指纹显然会改变,因此inage URL您之前发送的所有电子邮件都将失效。您可能希望考虑内联图像,尽管显然这些会增加电子邮件的大小
You're giving image_tag an absolute url so it thinks it doesn't need to do any fingerprinting or anything else other than regurgitate the string you gave it. I would try
You'll also need to set actioncontroller's asset host to get rails to generate a full url for the image rather than just a path
One caveat to note: if you change the image then the fingerprint will obviously change and so the inage URL in all of your previously sent emails will become invalid. You may wish to consider inline images, although obviously these increase the email size
试试这个
Try out this one
添加
mode:'rb'
对我有用:Adding
mode:'rb'
worked for me:如果您编译资产:
并在您的视图中使用 asset_path:
-- 它应该在开发和生产中都有效。这就是我的视图和 .css.scss.erb 样式表的做法。我认为它对于邮件发送者来说没有什么区别。
If you compile your assets:
and use asset_path in your view:
--it should work in both development and production. This is the way I do it my views, and .css.scss.erb stylesheets. I assume that it doesn't make a difference that it is a view for a mailer.
确保您的 html 页面具有以下标头
并将图像呈现为:
或者
如果您想要链接到图像,则
@url = '您的网站地址'
make sure your html page have follwing header
and render image as:
or
if you want link to image then
@url = 'your website address'