简体   繁体   中英

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

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 (eg 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

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 .

Why they dont show up on rake routes, I am not sure. But if you wanna know their alias you can find them in here: https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

From their docs:

Currently, Devise only allows you to make one model omniauthable. After making a model named User omniauthable and if "devise_for :users" was already added to your config/routes.rb, Devise will create the following url methods:

user_omniauth_authorize_path(provider) user_omniauth_callback_path(provider)

So if you have devise_for :model in your routes you should see that url method.

Example:

<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

Also if you take a look at the implementation of devise, you can see that the URL helpers are there:

https://github.com/plataformatec/devise/blob/master/lib/devise/omniauth/url_helpers.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM