gpt4 book ai didi

ruby-on-rails - OmniAuth 不适用于 Rails3 中的 Route Globbing

转载 作者:行者123 更新时间:2023-12-04 05:51:07 25 4
gpt4 key购买 nike

我正在尝试关注 Railscast 241 Simple OmniAuth它工作正常,除非我在 /config/routes.rb 末尾有 Route Globbing :

match '*uri' => "posts#index"

如果我要求 /auth/twitter使用通配符然后 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)

如果没有 globbing 路由,它会正确地进行身份验证。

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

最佳答案

The OmniAuth process是在/auth/:provider时提供以下功能网址被称为:

  • 将请求传递给底层 Rack/Rails 应用程序,就好像 OmniAuth 不存在一样;
  • 判断底层应用是否产生404;
  • 如果是,请调用实际的 OmniAuth 功能。

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

    手动将 OmniAuth 路由与 404 匹配

    添加新路由如下:
    match '/auth/:provider' => 'omniauth#passthru'

    然后创建一个生成 404 的 Controller 和 Action :
    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。
    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

    关于ruby-on-rails - OmniAuth 不适用于 Rails3 中的 Route Globbing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5531263/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com