devise icon indicating copy to clipboard operation
devise copied to clipboard

(Un)Authenticated Routes Are Broken in Rails 4.0.0.rc1

Open excid3 opened this issue 13 years ago • 11 comments

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.

excid3 avatar Apr 29 '13 16:04 excid3

I've run into this issue as well.

chooserich avatar Apr 30 '13 22:04 chooserich

I was also having this problem.

romaimperator avatar Apr 30 '13 22:04 romaimperator

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

josevalim avatar May 01 '13 18:05 josevalim

That makes sense, not a bad solution for now.

excid3 avatar May 02 '13 00:05 excid3

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!

josevalim avatar May 07 '13 14:05 josevalim

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 avatar Jul 08 '13 08:07 mackgille

@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 avatar Jul 08 '13 14:07 excid3

@excid3 thanks a lot.it's working fine.

mackgille avatar Jul 09 '13 10:07 mackgille

@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

ACPK avatar Mar 26 '14 23:03 ACPK

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.

ahappydad avatar Jul 13 '14 03:07 ahappydad

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

RodneyU215 avatar Jul 25 '14 15:07 RodneyU215