Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
excid3 opened this issue Apr 29, 2013 · 11 comments
Closed

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

excid3 opened this issue Apr 29, 2013 · 11 comments

Comments

@excid3
Copy link
Contributor

excid3 commented Apr 29, 2013

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.

@biznickman
Copy link

I've run into this issue as well.

@romaimperator
Copy link

I was also having this problem.

@josevalim
Copy link
Contributor

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

@excid3
Copy link
Contributor Author

excid3 commented May 2, 2013

That makes sense, not a bad solution for now.

@josevalim
Copy link
Contributor

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!

@mackgille
Copy link

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

@excid3
Copy link
Contributor Author

excid3 commented Jul 8, 2013

@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

@mackgille
Copy link

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

@ACPK
Copy link

ACPK commented Mar 26, 2014

@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

@ahappydad
Copy link

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.

@RodneyU215
Copy link

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

miyagawa pushed a commit to cookpad/garage that referenced this issue Sep 17, 2014
This is work around for this kind of issue
rails/rails#10418
heartcombo/devise#2393
frankieroberto added a commit to OfficeForProductSafetyAndStandards/product-safety-database that referenced this issue May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants