Rails 3路由约束和正则表达式 [英] Rails 3 Routing Constraint and Regex

查看:53
本文介绍了Rails 3路由约束和正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找匹配路径中的模式state/city的方法,除非状态变量等于"auth"

I'm looking to match the pattern state/city in the path, unless the state variable equals "auth"

match '/:state/:city' => 'cities#index', :as => :state_cities, :constraints => {:state => /(?!auth)/ }

例如,mydomain.com/fl/miami是好的. mydomain.com/auth/twitter不好.

For example, mydomain.com/fl/miami is good. mydomain.com/auth/twitter is bad.

我正在使用omniauth,它要求您转到/auth/twitter进行身份验证,但是当我键入rake routes时找不到它.

I am using omniauth and it requires that you go to /auth/twitter for authentication, however it is nowhere to be found when I type rake routes.

推荐答案

基于mu的评论太短了,这是我想出的答案:

Based on mu is too short's comments, here is the answer I've come up with:

match '/:state/:city' => 'cities#index', :as => :state_cities, :constraints => OmniauthPassThru.new

lib/omniauth_pass_thru.rb

lib/omniauth_pass_thru.rb

class OmniauthPassThru
    def initialize
        @passthru = ["/auth/facebook", "/auth/twitter"]
    end

    def matches?(request)
        return false if @passthru.include?(request.fullpath)
        true
    end
end

这篇关于Rails 3路由约束和正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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