如何在Rails异步邮件程序中获取图像的完整URL? [英] How do I get full URL to an image in a Rails asynchronous mailer?

查看:76
本文介绍了如何在Rails异步邮件程序中获取图像的完整URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用ActionMailer发送的电子邮件包含链接回我的应用程序的图像。在我的开发环境中,例如:

 < img src = http://myfullappurl.dev/assets/myimage。 png> 

我在开发中有此文件。rb

  config.action_controller.asset_host ='myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = {主机: 'myfullappurl.dev',only_path:false}

但是我无法获取邮件模板来呈现完整的URL,可以通过以下任何一种方式:

  asset_path('myimage.png')
asset_path('myimage.png ',only_path:false)
image_url('myimage.png')

很多通过执行以下操作可以回答与此主题类似的问题:

 #{request.protocol}#{request.host_with_port} #{asset_path('image.png')} 

但是因为我的邮件与Sidekiq异步发送,就没有 request 对象,就像如果它们是在控制器中同步发送一样。



是否有明显的东西我想念,还是我必须与Rails对抗?我正在使用Rails 4,但是我确信在3.1 / 3.2中可以正常使用的任何东西。

解决方案

是奇怪的行为,但我已经解决了这个问题。事实证明,除非action_mailer.asset_host以 http:// 开头,否则它将被忽略。 actionpack / lib / action_view / helpers / asset_url_helper.rb 中有一个正则表达式,用于定义有效的ActionMailer URI:

  URI_REGEXP =%r {^ [-az] +:// | ^(?:cid | data):| ^ //} 

但是,如果您在 http:// > action_controller.asset_host ,那么您最终将获得指向 http:// http://myfullappurl.dev

$ b $的链接b

要解决该问题,我必须在我的 development.rb

中添加以下内容

  config.action_controller.asset_host ='myfullappurl.dev'
config.action_mailer.asset_host ='http://myfullappurl.dev'

需要明确的是,这是Rails 4.0.0beta1,并且电子邮件与Sidekiq异步发送,我不确定这是否会影响Rails 3。 / p>

I want to have emails I send out with ActionMailer contain images that link back to my app. In my development environment, for example:

<img src="http://myfullappurl.dev/assets/myimage.png">

I have this in my development.rb

config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = { host: 'myfullappurl.dev', only_path: false }

But I can not get my mail templates to render a full URL in any of these way:

asset_path('myimage.png')
asset_path('myimage.png', only_path: false)
image_url('myimage.png')

A lot of similar questions on this topic are answered by doing something like this:

"#{request.protocol}#{request.host_with_port}#{asset_path('image.png')}"

But because my mails are sent asynchronously with Sidekiq, there is no request object like there would be if they were sent synchronously in a controller.

Is there an obvious thing I am missing, or do I have to fight against Rails to do this? I am using Rails 4, but I am sure that anything that works in 3.1/3.2 should do just fine.

解决方案

Well this is odd behaviour, but I've resolved this issue. It turns out that unless the action_mailer.asset_host starts with http:// then it will be ignored. There is a Regex in actionpack/lib/action_view/helpers/asset_url_helper.rb that defines a valid ActionMailer URI:

URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

However, if you put a http:// in front of the action_controller.asset_host then you will end up with links to http://http://myfullappurl.dev

So to resolve it I had to add the following to my development.rb

 config.action_controller.asset_host = 'myfullappurl.dev'
 config.action_mailer.asset_host = 'http://myfullappurl.dev'

Just to be clear, this is Rails 4.0.0beta1 with the emails being sent asynchronously with Sidekiq, I am not sure if this affects Rails 3.

这篇关于如何在Rails异步邮件程序中获取图像的完整URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆