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

ruby-on-rails – 如何处理带有公寓宝石的电子邮件中的网址

发布时间:2020-12-17 02:55:58 所属栏目:百科 来源:网络整理
导读:我正在使用公寓宝石进行多租户. 除了我的电子邮件中的网址,一切正常.因此,对于从任何请求和后台作业发送的每封电子邮件,都使用default_url_options作为主机. 有关如何处理电子邮件中的主机切换的任何建议? 解决方法 猴子补丁的另一个想法: # app/models/ac
我正在使用公寓宝石进行多租户.

除了我的电子邮件中的网址,一切正常.因此,对于从任何请求和后台作业发送的每封电子邮件,都使用default_url_options作为主机.

有关如何处理电子邮件中的主机切换的任何建议?

解决方法

猴子补丁的另一个想法:

# app/models/action_dispatch_routing_subdomain_extension.rb

module ActionDispatch::Routing

  module RouteSetExtensions

    # This allows lambdas as subdomain parameter for `default_url_options`:
    #
    #    config.action_mailer.default_url_options = {
    #      host: 'example.com',#      protocol: 'https',#      subdomain: lambda { ... }
    #    }
    #
    def url_for(options,route_name = nil,url_strategy = ActionDispatch::Routing::RouteSet::UNKNOWN)

      if options[:subdomain].respond_to? :call
        options[:subdomain] = options[:subdomain].call
      end

      if Rails.application.config.action_mailer.default_url_options[:subdomain].respond_to? :call
        options[:subdomain] ||= Rails.application.config.action_mailer.default_url_options[:subdomain].call
      end

      super(options,route_name,url_strategy)

    end
  end

  class RouteSet
    prepend RouteSetExtensions
  end

end

初始化:

# config/initializers/action_dispatch_routing_subdomain_extension.rb
require 'action_dispatch_routing_subdomain_extension'

然后,您可以在子域定义中使用lambda:

# config/environments/production.rb

Rails.application.configure do
  # ...

  config.action_mailer.default_url_options = {
    host: 'example.com',protocol: 'https',subdomain: lambda { Apartment::Tenant.current }
  }
end

(编辑:李大同)

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

    推荐文章
      热点阅读