2011-12-29 48 views
3

我有一個partners子域,它使用控制器爲內容提供服務。如何強制導軌創建到www子域的鏈接?

的網址是這樣的:http://partners.example.com/...

有沒有一種方法,我可以強制所有_path_url函數返回鏈接到根www域名?由於我的鏈接引用的資源不在子域本身上,只在根域上(我不想提供重複項)。

回答

7

在Rails 3.1,它是可以做到的:

resource_url(:subdomain => 'www') 

不過,助手resource_path不接受subdmain關鍵。

+3

'root_url(子域:'www')'也需要到達子域的主頁時才能正常工作。 – scarver2 2014-07-31 03:10:37

2

這與我的答案在這裏:How to automatically set all links to nofollow in Rails

而不是無以下(就像我的其他答案),你可以:

def link_to(name, options = {}, html_options = {}) 
    html_options.merge!(:host => with_subdomain("www")) 
    super(name, options, html_options) 
end 

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

with_subdomain方法是從http://railscasts.com/episodes/221-subdomains-in-rails-3

2

我設置default_url_options,包括您的主要宿主:

ActionController::Base.default_url_options = {:host => '...'} 

的選項傳遞給link_to將覆蓋這些,但顧名思義,這可以讓你提供默認值。

相關問題