如何使用资产管道在mailer中使用样式表的绝对路径? [英] How to have absolute path for stylesheets in mailer with the asset pipeline?
问题描述
我的Mailer模板中的视图帮助器为我提供了样式表和图像的相对URL.当然,例如,如果我正在Gmail中查看电子邮件,则无法使用.
The view helpers in my Mailer template give me relative URLs to the stylesheet and images. Of course, this won't work if I'm viewing the email in Gmail, for example.
在apps/views/layouts/mailer.html.erb
<%= stylesheet_link_tag "application" %>
...
<%= link_to(image_tag("logo.png"), "http://mysite.com") %>
渲染为:
<link href="/assets/application-c90478153616a4165babd8cc6f4a28de.css" media="screen" rel="stylesheet" type="text/css" />
...
<a href="http://mysite.com"><img alt="Logo" src="/assets/logo-d3adbf8d0a7f7b6473e2130838635fed.png" /></a>
我如何获得Rails来给我绝对链接?我在Rails 3.1上,资产管道已生效.
How do I get Rails to give me absolute links instead? I'm on Rails 3.1, the asset pipeline is in effect.
推荐答案
`config.action_controller.asset_host 处理从ActionController生成的视图中的主机前缀.
`config.action_controller.asset_host handles the host prefix in views generated from an ActionController.
对于电子邮件中生成的任何内容,您都在寻找 ActionMailer 配置选项,更具体地说:
For anything generated in an email you're looking for the ActionMailer configuration options, more specifically:
-
ActionMailer::Base.asset_host
将处理您的image_tags
和 -
ActionMailer::Base.default_url_options[:host]
将照顾您的link_to
标签.
ActionMailer::Base.asset_host
will handle yourimage_tags
andActionMailer::Base.default_url_options[:host]
will look after yourlink_to
tags.
例如:
ActionMailer::Base.asset_host = "http://blah.com"
ActionMailer::Base.default_url_options[:host] = "blah.com"
请注意,您无需为默认网址主机指定http前缀,而只需为资产主机指定http前缀.
我已在应用程序初始化程序后的 environment.rb 中指定了这些内容.我建议为每个环境域设置一个应用程序配置变量.
I have specified these inside my environment.rb after the application initializer. I would recommend setting an application configuration variable for each environments domain.
这篇关于如何使用资产管道在mailer中使用样式表的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!