我正在尝试遵循 Railscast 241简单的 OmniAuth除非我在末尾有路由通配,否则它工作得很好/config/routes.rb
:
match '*uri' => "posts#index"
如果我要求/auth/twitter
使用 globbing 那么 OmniAuth 什么也不做:
Started GET "/auth/twitter" for 127.0.0.1 at 2011-04-03 19:17:44 +0200
Processing by PostsController#index as HTML
Parameters: {"uri"=>"auth/twitter"}
Rendered posts/index.html.haml within layouts/application (9.0ms)
Completed 200 OK in 103ms (Views: 14.6ms | ActiveRecord: 0.7ms)
如果没有通配路由,它可以正确进行身份验证。
有没有办法同时拥有路由通配和 OmniAuth?
OmniAuth 流程是提供以下功能时/auth/:provider
网址称为:
- 将请求传递给底层 Rack/Rails 应用程序,就好像 OmniAuth 不存在一样;
- 判断底层应用是否生成了404;
- 如果是,则调用实际的 OmniAuth 功能。
既然你们本质上是匹配的一切使用您的路由通配,您的应用程序将永远不会给出 404,并且 OmniAuth 无法完成它的工作。我看到两个直接的选择。
手动将 OmniAuth 路由与 404 匹配
添加新路由如下:
match '/auth/:provider' => 'omniauth#passthru'
然后创建一个生成 404 的控制器和操作:
class OmniauthController < ApplicationController
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
确定全局路由中的 404 状态
我假设你的 glob 路由会以某种方式搜索与 URL 匹配的帖子;你可以错过(例如,当PostsController#index
找不到帖子)然后生成 404。
class PostsController < ApplicationController
def index
if @posts = Post.find_by_current_url_or_whatever
render 'index'
else
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)