2010-12-11 17 views
2

我從制定一個url幫手如下:如何使用URL傭工當前子域

account_confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) 

如何讓它創建與當前的子域,而不是僅僅的主要子域網址是什麼?

回答

3

嘗試傳遞:主機=>「yoursub.domain.com」

+1

我想他想讓它自動發生。 – 2010-12-11 22:41:47

13

在設計維基描述的主要解決方案不適用於設置任意工作子域,如果您在一個子域(或您的應用程序的根域)的請求期間觸發生成電子郵件,並且希望電子郵件中的鏈接引用不同的子域,則會出現問題。

使其工作的普遍接受的方式是給url_for幫手一個:subdomain選項。

# app/helpers/subdomain_helper.rb 

module SubdomainHelper 
    def with_subdomain(subdomain) 
    subdomain = (subdomain || "") 
    subdomain += "." unless subdomain.empty? 
    host = Rails.application.config.action_mailer.default_url_options[:host] 
    [subdomain, host].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 

下一步是至關重要的,我懷疑這是很多人被絆倒的地方(我知道我做過)。確保設計中加入以下代碼,當你在設計郵件模板做link_to混合您的新的子助手到其郵件的對象config/application.rb

config.to_prepare do 
    Devise::Mailer.class_eval do 
     helper :subdomain 
    end 
    end 

現在,你可以很容易地指定:subdomain選項。

link_to 'Click here to finish setting up your account on RightBonus', 
    confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :subdomain => @resource.subdomain)