邮件程序中的 image_tag 不使用 asset_host

发布于 2024-11-02 22:09:17 字数 748 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-11-09 22:09:17

我之前就错了。这是您需要的解决方案(直到rails 3.1,其中 asset_host 配置变得统一):

config.action_mailer.asset_host = "http://localhost:3000"

I was wrong before. This is the solution you need (until rails 3.1 where the asset_host configurations become unified):

config.action_mailer.asset_host = "http://localhost:3000"
情丝乱 2024-11-09 22:09:17

我们需要在 Rails 3.1 和 3.2 上指定 config.action_controller.asset_host 和 config.action_mailer.asset_host。

要将主机名添加到电子邮件和非电子邮件视图上的 image_tag,请将以下内容添加到您的环境文件中:

config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host

其中“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:

config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host

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.

心在旅行 2024-11-09 22:09:17

关于为什么你不能这样做的违规代码在这里:

# actionpack/lib/action_view/helpers/asset_paths.rb, line 27
def compute_public_path(source, dir, ext = nil, include_host = true)
  # More code up here....

    if controller && include_host
      has_request = controller.respond_to?(:request)
      source = rewrite_host_and_protocol(source, has_request)
    end
end

这是 GH 上的违规文件: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/asset_paths.rb

由于 ActionMailer 视图模板缺少控制器,您没有得到基于 asset_host 重写的命令。这可能应该是向 Rails 核心团队开出的一张票。

您可以尝试以下配置,看看它是否有帮助:

config.action_mailer.default_url_options = {:host=>"localhost", :port=>3000, :protocol=>"http://"}

不过我很确定它只适用于 url_for

The offending code as to why you can't do it is here:

# actionpack/lib/action_view/helpers/asset_paths.rb, line 27
def compute_public_path(source, dir, ext = nil, include_host = true)
  # More code up here....

    if controller && include_host
      has_request = controller.respond_to?(:request)
      source = rewrite_host_and_protocol(source, has_request)
    end
end

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:

config.action_mailer.default_url_options = {:host=>"localhost", :port=>3000, :protocol=>"http://"}

I'm pretty sure it's only going to work for url_for though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文