2017-06-04 8 views
0

私のアプリケーションでは、パブリックサイト(サブドメイン:www)のユーザーをリンク先ページに、サブドメインのユーザーをダッシュ​​ボードにルーティングしたいと考えています。だから私はこれを行うためのさまざまな方法を試し、私はこれをしようとしたとき:routes.rbに2つのルートパスを定義できるのはなぜですか?

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

すべては私が期待したものではないあるうまく働きました。 root to: 'examplecontroller#index'get '/', to: 'examplecontroller#index', as: :rootに展開され、同じ名前(asオプション)の2つのルートを定義すると、エラーが発生するはずです。

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 
関連する問題