Rails:根据域​​在不同的root_to路由之间进行选择

时间:2017-09-15 12:20:00

标签: ruby-on-rails ruby-on-rails-4 routes

我的想法是,如果你去www.my-website.com,我想要显示一个"选择国家"登录页面,包含我所在国家/地区网站的链接,如www.my-website.dk,www.my-website.de等。

我做了一个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

问题是,我不能使用rails 4.2多个root_to,那么如何更改routes.rb以实现此行为?

1 个答案:

答案 0 :(得分: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