OmniAuth不适用于Rails3中的Route Globbing [英] OmniAuth doesn't work with Route Globbing in Rails3
问题描述
我正在尝试遵循Railscast 241 简单的OmniAuth ,除非我能正常工作/config/routes.rb
的末尾具有路由通配符":
I am trying to follow the Railscast 241 Simple OmniAuth and it works fine unless I have Route Globbing at the end of /config/routes.rb
:
match '*uri' => "posts#index"
如果我请求/auth/twitter
进行通配,则OmniAuth将不执行任何操作:
If I request /auth/twitter
with the globbing then OmniAuth does nothing:
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)
没有通行路径,它就可以正确地进行身份验证.
Without the globbing route it authenticates correctly.
是否可以同时使用路由和OmniAuth?
Is there a way to have both the route globbing and OmniAuth?
推荐答案
The OmniAuth process is to provide the following functionality when a /auth/:provider
URL is called:
- 将请求传递到基础的Rack/Rails应用程序,就像没有OmniAuth一样;
- 确定基础应用程序是否生成了404;
- 如果这样做,请调用实际的OmniAuth功能.
由于本质上是使用路由全局匹配来匹配所有,因此您的应用程序将永远不会给出404,并且OmniAuth无法完成它的工作.我看到了两个即时选择.
Since you are essentially matching everything using your route globbing, your application will never give 404's, and OmniAuth cannot do it's job. I see two immediate options.
添加新路线,如下所示:
Add a new route as follows:
match '/auth/:provider' => 'omniauth#passthru'
然后创建一个控制器和一个生成404的动作:
Then create a controller and action that generates a 404:
class OmniauthController < ApplicationController
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
确定全球路由中的404状态
我假设您的全局路由将以某种方式搜索与URL匹配的帖子;您可以错过(例如,当PostsController#index
找不到帖子时),然后生成404.
Determine 404 Status in the Glob Route
I assume that your glob route will search for a post matching the URL somehow; you can take misses (e.g. when PostsController#index
can't find a post) and generate 404's then.
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
这篇关于OmniAuth不适用于Rails3中的Route Globbing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!