简体   繁体   English

Rake路线不显示路线,但在硬编码时存在

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

I'm using Devise and Omniauth for my login process. 我正在使用Devise和Omniauth进行登录。 For some reason, I can access the route "users/auth/facebook" or "users/auth/twitter" just fine. 出于某种原因,我可以访问路线“users / auth / facebook”或“users / auth / twitter”就好了。 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). 但是当我做rake路由时它们不显示,所以我不知道获取这些路径的辅助方法是什么(例如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): 我无法显示我的所有路线,但我可以说匹配“/ users / auth / ...”的唯一路线就是这个(来自rake路线):

 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): 顺便说一句,当我说“我可以很好地访问路线”时,我的意思是这样做(将我正确地重定向到facebook或twitter):

<%= link_to "Connect", "users/auth/facebook" %>

Also, the routes should be the default Devise omniauth routes for the user model 此外,路由应该是用户模型的默认Devise omniauth路由

With regard to why this doesn't show up in rake routes , first note how the task is implemented. 关于为什么这不会出现在rake routes ,首先要注意任务是如何实现的。 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. 所以我们可以看到它正在向Rails.application询问它的路由。


Next note that the Omniauth gem "is a flexible authentication system utilizing Rack middleware". 接下来请注意, Omniauth gem “是一个灵活的使用Rack中间件的身份验证系统”。

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. 因为它使用Rack中间件,它不会“了解”有关rake routes使用的Rails.application的任何信息,因此它的路由不会出现在该任务中。

You can get a good introduction to Rack middleware in this Railcast . 您可以在此Railcast中获得Rack中间件的良好介绍。


Delving a little deeper we can see from rake middleware that OmniAuth::Builder appears before your rails app in the stack. 深入研究我们可以从rake middleware看到OmniAuth::Builder出现在堆栈中的rails应用程序之前。 So how does it handle the auth/twitter route? 那么它如何处理auth/twitter路由?

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 . 它通过检查一个request_pathcall中,您可以看到支票在这里 ,你可以看到如何request_path是建立在这里path_prefixauth默认情况下,并name你的情况是twitter

When using Omniauth with Devise, the path_prefix is set automatically, as noted here . 使用带有Devise的path_prefix会自动设置path_prefix ,如此处所示

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 但如果您想知道他们的别名,您可以在这里找到它们: https//github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

From their docs: 从他们的文档:

Currently, Devise only allows you to make one model omniauthable. 目前,Devise只允许您制作一个可以忽略的模型。 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 omniauthable的模型并且如果已将“devise_for:users”添加到config / routes.rb之后,Devise将创建以下url方法:

user_omniauth_authorize_path(provider) user_omniauth_callback_path(provider) 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. 因此,如果你的路线中有devise_for :model ,你应该看到url方法。

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: 另外,如果你看一下devise的实现,你可以看到URL帮助器在那里:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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