【问题标题】:Rails 3.1 - in routes.rb, using the wildcard, match '*' is not catching any routesRails 3.1 - 在 routes.rb 中,使用通配符,匹配 '*' 没有捕获任何路线
【发布时间】:2012-05-09 23:35:13
【问题描述】:

在我的pages_controller.rb 文件中,我有一个not_found 操作。我想让任何与我的routes.rb 文件中的条目不匹配的路由转到pages#not_found。为了利用通配符,我将其作为 routes.rb 文件的最后一行:

match '*' => 'pages#not_found'

这是基于我在 ruby​​ 路由指南 (http://guides.rubyonrails.org/v3.1.0/routing.html) 中读到的有关通配符的内容。但是它不起作用。我没有去pages#not_found,而是得到:AcctionController::RoutingError (No route matches [GET] "*"(不幸的是,它被 ActionDispatch 捕获,所以我没有办法处理异常。)

疑难解答,我把它放在了我的 routes.rb 文件中:

match 'foobar' => 'pages#not_found'

not_found 操作按预期在http://localhost:3000/foobar 上执行。我尝试了部分通配符:

match 'foo*' => 'pages#not_found'

根据 Rails 指南,这应该与 http://localhost:3000/foobar 匹配,但事实并非如此。您是否需要做一些特别的事情才能让通配符在路线中起作用?我错过了什么?

【问题讨论】:

    标签: ruby-on-rails-3 exception-handling routing wildcard


    【解决方案1】:

    也许试试这个:

    match '*path' => 'pages#not_found'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-03
      • 2011-10-29
      • 2012-10-11
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多