Rake routes not showing the route, but it exists when hardcoded

devise, omniauth, routes, ruby-on-rails, ruby-on-rails-3

Solution

With regard to why this doesn't show up in `rake routes`, first note how the task is implemented. It is part of railties, and it gets the routes to show as such:

Rails.application.routes.routes

So we can see that it is asking the `Rails.application` for its routes.

Next note that the Omniauth gem "is a flexible authentication system utilizing Rack middleware".

Because it uses Rack middleware it does not 'know' anything about the `Rails.application` used by `rake routes`, and so its routes don't appear in that task.

You can get a good introduction to Rack middleware in this Railcast.

Delving a little deeper we can see from `rake middleware` that `OmniAuth::Builder` appears before your rails app in the stack. So how does it handle the `auth/twitter` route?

It does so by checking for a `request_path` in its `call`, you can see the check here, and you can see how the `request_path` is built here (`path_prefix` is `auth` by default, and `name` in your case is `twitter`.

When using Omniauth with Devise, the `path_prefix` is set automatically, as noted here.

Problem

I'm using Devise and Omniauth for my login process. For some reason, I can access the route "users/auth/facebook" or "users/auth/twitter" just fine. But they don't show up when I do rake routes, so I have no idea what the helper method to get these paths is (e.g. something_something_path). Can someone help me out? I can't show all of my routes, but I can say that the only route that matches "/users/auth/..." that's showing up is this one (from rake routes): ``` user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"users/omniauth_callbacks"} ``` BTW, when I say I "can access the route just fine", I mean this works (redirects me correctly to facebook or twitter): ``` <%= link_to "Connect", "users/auth/facebook" %> ``` Also, the routes should be the default Devise omniauth routes for the user model

Original source