ruby-on-railsruby-on-rails-4rails-routingroute-constraint

Cant define root route more than once in Rails 4


I've got the following in my routes file:

scope :constraints => lambda{ |req| req.session[:user_id].present? } do
  root "users#show"  
end

scope :constraints => lambda{ |req| req.session[:admin_id].present? } do
  root "brands#index"  
end

root "sessions#new"

This code worked fine in Rails 3, but when I use it in Rails 4 I get the following error message:

Invalid route name, already in use 'root' (ArgumentError).

You may have defined two routes with the same name using the ':as' option

Is there a way round this? What has changed?


Solution

  • As @vimsha pointed out, it's a known issue and in my case the best fix was to do the following:

    scope :constraints => lambda{ |req| req.session[:user_id].present? } do
      match '/', to: "users#index", via: :get  
    end
    
    scope :constraints => lambda{ |req| req.session[:admin_id].present? } do
      match '/', to: "brands#index", via: :get  
    end
    
    root "sessions#new"
    

    Alles im ordinem.