邮件程序中的 image_tag 不使用 asset_host
image_tag 没有使用我设置的 asset_host 。有什么想法吗?我唯一能想到的是它与邮件程序有关。
config/environment/development.rb
config.action_controller.asset_host = "http://localhost:3000"
myMailer.rb
<%= image_tag "logo.png", :style=>"margin-left:10px; padding-bottom:15px;" %>
渲染为:
<img alt="Logo" src="/images/logo.png?1303090162" style="margin-left:10px; padding-bottom:15px;" />
在控制台中:
> MyApp::Application.config.action_controller
#<OrderedHash {… :asset_host=>"http://localhost:3000", …}>
我需要 image_tag 来创建完整路径 url,因为它将显示在电子邮件中。
image_tag isn't using the asset_host I've set. Any ideas why? The only thing I can think of is it having to do with it being a Mailer.
config/environment/development.rb
config.action_controller.asset_host = "http://localhost:3000"
myMailer.rb
<%= image_tag "logo.png", :style=>"margin-left:10px; padding-bottom:15px;" %>
rendered as:
<img alt="Logo" src="/images/logo.png?1303090162" style="margin-left:10px; padding-bottom:15px;" />
In console:
> MyApp::Application.config.action_controller
#<OrderedHash {… :asset_host=>"http://localhost:3000", …}>
I need the image_tag to create a full path url because it will be showing up in an email.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我之前就错了。这是您需要的解决方案(直到rails 3.1,其中 asset_host 配置变得统一):
I was wrong before. This is the solution you need (until rails 3.1 where the asset_host configurations become unified):
我们需要在 Rails 3.1 和 3.2 上指定 config.action_controller.asset_host 和 config.action_mailer.asset_host。
要将主机名添加到电子邮件和非电子邮件视图上的 image_tag,请将以下内容添加到您的环境文件中:
其中“http://localhost:3000”应替换为您的主机 URL(以及端口,如果适用)。
即使在 Rails 3.2.x 中,也需要在 action_controller 和 action_mailer 上进行设置。
We need to specify both config.action_controller.asset_host and config.action_mailer.asset_host, on Rails 3.1 and 3.2.
To add the hostname to the image_tag on both e-mail and non-email views, add the following to your environment file:
Where 'http://localhost:3000' should be replaced by your host URL (and port if applicable).
This needs to be set on both action_controller and action_mailer, even in Rails 3.2.x.
关于为什么你不能这样做的违规代码在这里:
这是 GH 上的违规文件: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/asset_paths.rb
由于 ActionMailer 视图模板缺少控制器,您没有得到基于 asset_host 重写的命令。这可能应该是向 Rails 核心团队开出的一张票。
您可以尝试以下配置,看看它是否有帮助:
不过我很确定它只适用于
url_for
。The offending code as to why you can't do it is here:
Here is the offending file on GH: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/asset_paths.rb
Since an ActionMailer View template lacks a Controller, you don't get the command to rewrite based on an asset_host. This should probably be a ticket opened to the Rails core team.
You can try the following config and see if it helps:
I'm pretty sure it's only going to work for
url_for
though.