2011-08-23 42 views
1

我試圖得到這個工作,但它一直使用子域名我已經在http://railscasts.com/episodes/221-subdomains-in-rails-3子域在Rails 3中的網址助手

在url_for當我調試,它表明選項[:主機]作爲什麼我期望(subdomain.domain.com),但超級只是返回'accounts/sign_up'沒有完整的路徑。那感覺不對。

這是怎麼回事?下面是我有:

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 

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

end 

我試圖色器件和非設計助手:

= link_to 'Plan', new_plan_path(:subdomain => 'mysubdomain') 
    = link_to "Sign up", new_registration_path(resource_name, :subdomain => 'mysubdomain'); 

UPDATE: 當我跟隨的代碼,它最終調用:

_routes.url_for((options || {}).reverse_merge!(url_options).symbolize_keys) 

其中

_routes.url_for(options || {}) # -> "/accounts/sign_in" 

(url_options).symbolize_keys # -> {:host=>"test.lvh.me:3000", :protocol=>"http://", :_path_segments=>{:action=>"new", :controller=>"devise/passwords"}, :script_name=>""} 

仍然不知道如何解決它。

+0

軌道3.0.x和rails 3.1.x? –

+1

如果你使用'super(options)' – apneadiving

+0

rails 3.0.7。super(options)也不會這樣做 – 99miles

回答

4

原來我想用「路徑」幫手,而不是‘URL’助理:(。因此,爲了使這項工作,你需要使用root_url,不root_path等