OmniAuth不适用于Rails3中的Route Globbing

And*_*rei 7 routes glob ruby-on-rails omniauth ruby-on-rails-3

我试图按照Railscast 241 Simple OmniAuth进行操作 ,除非我在下面结束时使用Route Globbing /config/routes.rb:

match '*uri' => "posts#index"
Run Code Online (Sandbox Code Playgroud)

如果我要求/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)
Run Code Online (Sandbox Code Playgroud)

没有通配路线,它可以正确验证.

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

Mic*_*ley 17

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

  1. 将请求传递给底层的Rack/Rails应用程序,就像OmniAuth不在那里一样;
  2. 确定底层应用程序是否生成404;
  3. 如果是,请调用实际的OmniAuth功能.

由于你基本上使用你的路由通配符匹配所有内容,你的应用程序永远不会给404,OmniAuth无法完成它的工作.我看到两个直接的选择.

手动将OmniAuth路由与404匹配

添加新路线如下:

match '/auth/:provider' => 'omniauth#passthru'
Run Code Online (Sandbox Code Playgroud)

然后创建一个生成404的控制器和操作:

class OmniauthController < ApplicationController
  def passthru
    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end
end
Run Code Online (Sandbox Code Playgroud)

确定Glob Route中的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
Run Code Online (Sandbox Code Playgroud)