2017-06-04 119 views
0

在我的應用程序中,我希望將公共站點上的用戶(子域:www)路由到登錄頁面和子域上的用戶到他們的儀表板。於是我嘗試用不同的方式來做到這一點,當我嘗試這樣做:爲什麼可以在routes.rb中定義兩個根路徑?

root to: 'pages#landingpage', constraints: { subdomain: 'www' } 
root to: 'dashboard#index' 

一切正常這是我所期待的。由於據我瞭解root to: 'examplecontroller#index'擴展到get '/', to: 'examplecontroller#index', as: :root,並定義兩個具有相同名稱(as選項)的路線應該引發一個錯誤。

爲什麼在使用root方法時不會發生這種情況,我錯過了什麼?

回答

0

好吧,我想通了。 通常我描述的行爲是正確的。也就是說,如果你定義兩次具名的路線則出現錯誤:

 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" 

但是,顯然已經有些discussion過這種行爲是否應適用於根線路,並加入一個方法,以防止多根中的聲明defining routes with the same name

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 
相關問題