(Un)Authenticated Routes Are Broken in Rails 4.0.0.rc1
The route name clashing functionality is causing the authenticated and related methods to break now since we cannot define multiple routes with the same name.
The relevant commit in is this one rails/rails@822dd13 with a discussion in rails/rails#9704 and a good example of it here that crashes:
authenticated :user do
root to: "users#index"
end
root to: "main#index"
which raises this exception:
ArgumentError: Invalid route name, already in use: 'root'
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
Even wrapping the second root clause with unauthenticated didn't seem to do the trick like I thought it would. My thoughts are that having the following would be the best solution:
authenticated :user do
root to: "users#index"
end
unauthenticated do
root to: "main#index"
end
Wasn't sure if this belonged as an issue in Devise or Rails though.
I've run into this issue as well.
I was also having this problem.
For now, you will need to give the second root a different named route:
authenticated :user do
root to: "users#index", as: :authenticated_root
end
unauthenticated do
root to: "main#index"
end
That makes sense, not a bad solution for now.
This is a Rails change of behaviour and Rails is not going back. I will update the docs accordingly. If you guys have seen this mentioned in the wiki, would you be kind to update the wiki too? Thank you!
let me know,when i am using two devises how to use the routes for my user and admin models. how can i use the below toutes for two devises authenticated :user do root to: "users#index", as: :authenticated_root end
unauthenticated do root to: "main#index" end
@mackgille just remember to use as: on each root url to describe it. You can use any name you like, the only requirement is they all need to have different names. I chose unauthenticated_root for you, but you can use whatever you like.
authenticated :user do
root to: "users#index", as: :authenticated_root
end
unauthenticated do
root to: "main#index", as: :unauthenticated_root
end
@excid3 thanks a lot.it's working fine.
@excid3 @mackgille
I believe you meant:
authenticated :user do
root to: "users#index", as: :authenticated_root
end
unauthenticated do
root to: "main#index", as: :unauthenticated_root
end
Thanks for this. It might be worth noting to some newbies, like myself, that you must have at least one root entry un-named. Otherwise, you will start receiving "undefined local variable or method `root_path'" errors.
Thanks for this solution, incase someone stumbles here trying to root unauthenticated users to the devise sign up view like me, I was able to do it this way.
unauthenticated do
devise_scope :user do
root to: "devise/registrations#new", as: :unauthenticated_root
end
end