url_for in ActionMailer
url_for in ActionMailer
ActionMailer是Rails中发送email的一个controller。今天遇到的一个问题就是在ActionMailer controller/viewer中生成URL。我说的生成URL主要指url_for方法生成绝对路径,也就是 url_for(:only_path => false, :controller => xxx, :action => xxx)。我们都知道组成一个绝对路径需要有protocol, host, port和相对路径。在ActionMailer里面, request instance是不存在的,所以组成绝对路径的protocol, host 和 port是得不到的。
动态语言在AOP方面有着天然的优势。上面的问题可以很方面用一个around filter来解决。下面是代码片断
module InstanceMethods
def url_for_everywhere
begin
request = self.request
::ActionController::UrlWriter.module_eval do
@old_default_url_options = default_url_options.clone
default_url_options[:host] = request.host
default_url_options[:port] = request.port unless request.port == 80
protocol = /(.*):\/\//.match(request.protocol)[1] if request.protocol.ends_with?("://")
default_url_options[:protocol] = protocol
end
yield
ensure
::ActionController::UrlWriter.module_eval do
default_url_options[:host] = @old_default_url_options[:host]
default_url_options[:port] = @old_default_url_options[:port]
default_url_options[:protocol] = @old_default_url_options[:protocol]
end
end
end
end
思路很简单,::ActionController::UrlWriter中有默认的default_url_options的一些参数,filter选把这些参数保存起来,尝试去取当前request对应的参数,如果有那么应用,并继续执行后面的程序(url_for就在这里),最后需要把最原始的default_url_options restore。通过这样一个around filter我们就可以透明地解决ActionMailer中url_for不能使用的问题。当然也可以把它插件化
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 19522 次
- 性别:

- 来自: Mars

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Share methods in Control ...
这个,我就尽量不用,或者找模型来代理,尽量让controller变得简单,至少m ...
-- by lgn21st -
Share methods in Control ...
原则上,helper产生的返回值都是<div>....</div>这 ...
-- by 7thbyte -
Share methods in Control ...
helper是为template准备的,在controller中调用helper ...
-- by zeroleonhart -
Share methods in Control ...
在RoR2.0的class ApplicationController里有一句 ...
-- by baryon -
RXML
xu_wccq 写道发现个好方法先把xml转人为Hash表(from_xml(p ...
-- by xu_wccq






评论排行榜