Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3路由约束和正则表达式_Ruby On Rails_Regex_Routing_Omniauth - Fatal编程技术网

Ruby on rails Rails 3路由约束和正则表达式

Ruby on rails Rails 3路由约束和正则表达式,ruby-on-rails,regex,routing,omniauth,Ruby On Rails,Regex,Routing,Omniauth,我希望匹配路径中的模式state/city,除非state变量等于“auth” 例如,mydomain.com/fl/miami就很好mydomain.com/auth/twitter不正确 我正在使用omniauth,它要求您转到/auth/twitter进行身份验证,但是当我键入rake routes时,您应该能够在州/城市路线之前定义您的/auth路线: 并非所有路线都是平等创建的。路由的优先级由config/Routes.rb文件中路由的出现顺序定义。优先级从上到下 因此,这个订单应该

我希望匹配路径中的模式
state/city
,除非state变量等于“auth”

例如,
mydomain.com/fl/miami
就很好
mydomain.com/auth/twitter
不正确


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

时,您应该能够在州/城市路线之前定义您的
/auth
路线:

并非所有路线都是平等创建的。路由的优先级由config/Routes.rb文件中路由的出现顺序定义。优先级从上到下

因此,这个订单应该做正确的事情:

match '/auth/twitter' => ...
match '/:state/:city' => ... 
您可能希望通过将州/城市路线放入其自己的名称空间来完全避免此问题:

match '/place/:state/:city' => ...

这就为将来的其他用途保留了顶层空间。

您应该能够在州/城市路线之前定义您的
/auth
路线:

并非所有路线都是平等创建的。路由的优先级由config/Routes.rb文件中路由的出现顺序定义。优先级从上到下

因此,这个订单应该做正确的事情:

match '/auth/twitter' => ...
match '/:state/:city' => ... 
您可能希望通过将州/城市路线放入其自己的名称空间来完全避免此问题:

match '/place/:state/:city' => ...

这就为将来的其他用途留下了清晰的顶层。

根据mu的评论,我给出了以下答案:

match '/:state/:city' => 'cities#index', :as => :state_cities, :constraints => OmniauthPassThru.new
lib/omniauth\u pass\u 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

根据mu is To short的评论,以下是我得出的答案:

match '/:state/:city' => 'cities#index', :as => :state_cities, :constraints => OmniauthPassThru.new
lib/omniauth\u pass\u 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

我不能将它们放在自己的名称空间中,因为这是一个虚荣的url。我也不确定优先级是怎么回事,因为我使用的是omniauth gem,它在Rack中运行。事实上,我的
match:state/:city
已位于routes文件的最后一行。您可能希望更新您的问题,让人们知道您正在使用omniauth。我不知道omniauth在做什么。您是否尝试过使用来排除
/auth
?不,我没有使用约束对象,我希望内联正则表达式可以做到这一点,但似乎没有。我无法将它们放在自己的命名空间中,因为这是一个虚荣的url。我也不确定优先级是怎么回事,因为我使用的是omniauth gem,它在Rack中运行。事实上,我的
match:state/:city
已位于routes文件的最后一行。您可能希望更新您的问题,让人们知道您正在使用omniauth。我不知道omniauth在做什么。您是否尝试过使用排除
/auth
?不,我没有使用约束对象,我希望内联正则表达式可以做到这一点,但似乎没有。使用
可能会更好!request.fullpath.在
匹配项中以“('/auth/”)
开头,最好保留整个
/auth
命名空间。使用
可能会更好!request.fullpath.start_以(“/auth/”)
在您的
匹配中?
,最好保留整个
/auth
命名空间。