OmniAuth 不适用于 Rails3 中的 Route Globbing [英] OmniAuth doesn't work with Route Globbing in Rails3

查看:13
本文介绍了OmniAuth 不适用于 Rails3 中的 Route Globbing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循 Railscast 241 Simple OmniAuth 并且它工作正常,除非我在 /config/routes.rb 的末尾有 Route Globbing:

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)

如果没有 globbing 路由,它可以正确进行身份验证.

Without the globbing route it authenticates correctly.

有没有办法同时拥有路由通配符和 OmniAuth?

Is there a way to have both the route globbing and OmniAuth?

推荐答案

OmniAuth 过程是在调用 /auth/:provider URL 时提供以下功能:

The OmniAuth process is to provide the following functionality when a /auth/:provider URL is called:

  1. 将请求传递给底层 Rack/Rails 应用,就好像 OmniAuth 不存在一样;
  2. 确定底层应用程序是否生成了 404;
  3. 如果是,请调用实际的 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

确定 Glob 路由中的 404 状态

我假设您的 glob 路由会以某种方式搜索与 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆