Rails3:重写url_for以获得子域支持,如何扩展动作邮件程序以使用这样的url_for

pet*_*hka 4 subdomain ruby-on-rails actionmailer url-for

我从Subdomain RailsCast获取代码

module UrlHelper
  def with_subdomain(subdomain)
    subdomain = (subdomain || "")
    subdomain += "." unless subdomain.empty?
    [subdomain, request.domain, request.port_string].join
  end

  def url_for(options = nil)
    if options.kind_of?(Hash) && options.has_key?(:subdomain)
      options[:host] = with_subdomain(options.delete(:subdomain))
    end
    super
  end
end

class ApplicationController < ActionController::Base
  include UrlHelper
end  
Run Code Online (Sandbox Code Playgroud)

可以url_for在Controllers的视图中使用修改.但我在使用ActionMailer时遇到了麻烦.

我尝试使用以下内容:

class Notifier < ActionMailer::Base
  include UrlHelper
end
Run Code Online (Sandbox Code Playgroud)

但是ActionMailer视图仍然使用来自ActionDispatch :: Routing :: RouteSet的旧的未修改的url_for.

添加新url_for的最佳做法是什么?

Boo*_*age 5

将以下代码添加到文件app/helpers/url_helper.rb:

def set_mailer_url_options
    ActionMailer::Base.default_url_options[:host] = with_subdomain(request.subdomain)
end
Run Code Online (Sandbox Code Playgroud)

并修改文件app/controllers/application_controller.rb以添加:

before_filter :set_mailer_url_options
Run Code Online (Sandbox Code Playgroud)

资源