Ruby on rails 为什么可以在routes.rb中定义两个根路径?

Ruby on rails 为什么可以在routes.rb中定义两个根路径?,ruby-on-rails,Ruby On Rails,在我的应用程序中,我希望将公共站点(子域:www)上的用户路由到登录页,并将子域上的用户路由到其仪表板。所以我尝试了不同的方法,当我尝试时: root to: 'pages#landingpage', constraints: { subdomain: 'www' } root to: 'dashboard#index' 一切都很好,这不是我所期望的。因为根据我的理解,root到:'examplecontroller#index'扩展到get'/',再扩展到:'examplecontrolle

在我的应用程序中,我希望将公共站点(子域:www)上的用户路由到登录页,并将子域上的用户路由到其仪表板。所以我尝试了不同的方法,当我尝试时:

root to: 'pages#landingpage', constraints: { subdomain: 'www' }
root to: 'dashboard#index'
一切都很好,这不是我所期望的。因为根据我的理解,
root到:'examplecontroller#index'
扩展到
get'/',再扩展到:'examplecontroller#index',as::root
,定义两个同名路由(
as
选项)应该会出错

为什么在使用
root
方法时不会发生这种情况,我缺少了什么?

好的,我找到了答案。 通常我描述的行为是正确的。也就是说,如果两次定义命名路由,则会引发错误:

      raise ArgumentError, "Invalid route name, already in use: '#{name}' \n" \
        "You may have defined two routes with the same name using the `:as` option, or " \
        "you may be overriding a route already defined by a resource with the same naming. " \
        "For the latter, you can restrict the routes created with `resources` as explained here: \n" \
        "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created"
但是,对于是否应将此行为应用于根路由,显然存在一些争议,并且添加了一个方法来防止多个根声明:

def match_root_route(options)
  name = has_named_route?(name_for_action(:root, nil)) ? nil : :root
  args = ["/", { as: name, via: :get }.merge!(options)]

  match(*args)
end