2015-11-26 17 views
1

我學習軌和下載的一些軌道工程(軌道3.2.6),它在routes.rb中這樣的條目:兩個資源的一個途徑名稱

require 'logged_in_constraint' 

VkontakteOnRails::Application.routes.draw do 
    get 'callback' => 'sessions#callback' 
    delete 'logout' => 'sessions#destroy' 

    root to: 'main#index', constraints: LoggedInConstraint.new 
    root to: 'sessions#new' 
end 

,基於用戶是簡單是否登錄或根目錄路徑導致不同的控制器。我對嗎? 但是,如果我變更軌道項目版本4.2.1,我得到錯誤的服務器啓動:

Exiting 
/Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:555:in `add_route': Invalid route name, already in use: 'root' (ArgumentError) 
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: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1561:in `add_route' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1536:in `decomposed_match' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1517:in `block in match' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1507:in `each' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1507:in `match' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:387:in `root' 
    from /Users/khataev/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:1580:in `root' 
    from /Users/khataev/Documents/development/rails/vkontakte_on_rails/config/routes.rb:9:in `block in <top (required)>' 
.... 

這是否意味着新軌道不支持此功能了嗎?

+0

不能提供root,即使你有約束將有第二根兩次會始終適用。 – Tobias

回答

1

包括您可以添加as:選項,第二根路徑:

VkontakteOnRails::Application.routes.draw do 
    # some code here 
    root to: 'main#index', constraints: LoggedInConstraint.new 
    root to: 'sessions#new', as: :unauth 
end 
0

每個應用程序只能使用一個root。如果不是在namespace

root to: "home#index" 

namespace :admin do 
    root to: "admin#index" 
end 
+0

那麼,爲什麼在rails 3.2.6中可能呢? –

+1

https://github.com/rails/rails/issues/10494 – xxx

相關問題