2017-09-15 20 views
0

我的想法是,如果您訪問www.my-website.com,我想顯示一個「選擇國家/地區」登錄頁面,並鏈接到我的各個國家/地區的網站,例如www。 my-website.dk,www.my-website.de等等,等等Rails:根據域​​名選擇不同的root_to路由

我犯了這樣的一個的routes.rb:

MyApp::Application.routes.draw do 
    constraints subdomain: /\Awww\b/ do 
    apipie # For API documentation, see initializers/apipie.rb 
    draw :api_v1_v2_v3 
    draw :api 

    constraints ->(request) {request.host =~ /my-website\.com/ } do 
     root to: 'my_website_dot_com#landing_page' # anyone going to http://www.my-website.com/ will get this action 
    end 

    root to: 'my_website#front_page' 

    get 'sitemap', to: 'my_websiteouncle#sitemap' # anyone going to http://www.my-website.de/ or http://www.my-website.dk/ etc. will get this action 
    end 
end 

問題是,我不能有多個root_to與軌4.2因此如何我可以更改routes.rb來實現這種行爲嗎?

+0

爲什麼不加在測試request.url爲您的子域名,並相應地重定向應用程序控制器的方法? –

回答

0

我落得這樣做:

constraints ->(request) { request.host =~ /my-website\.com$/ } do 
    get '/' => 'my_website_dot_com#landing_page', locale: :en 
end 

constraints ->(request) { !(request.host =~ /my-website\.com$/) } do 
    root to: 'my_website#front_page' 
end