加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails3:重写url_for以获得子域支持,如何扩展a

发布时间:2020-12-17 02:59:14 所属栏目:百科 来源:网络整理
导读:我从 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 op
我从 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

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

我尝试使用以下内容:

class Notifier < ActionMailer::Base
  include UrlHelper
end

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

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

解决方法

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

def set_mailer_url_options
    ActionMailer::Base.default_url_options[:host] = with_subdomain(request.subdomain)
end

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

before_filter :set_mailer_url_options

Source

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读