Ruby-on-rails Rails 全能路线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13388715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:01:20  来源:igfitidea点击:

Rails catch-all route

ruby-on-railsrubyruby-on-rails-3

提问by Evil Nodoer

I am using rails 3.0.14, and I am constructing routes.rb using the resourceful style. I'd like to have a wildcard route that catches all requests that do not match to any route stated.

我正在使用 rails 3.0.14,我正在使用足智多谋的风格构建 routes.rb。我想要一个通配符路由来捕获所有与任何指定路由都不匹配的请求。

What's the appropriate way to construct such a route?

构建这样一条路线的合适方法是什么?

回答by awenkhh

put

match '*path' => 'your_controller#your_action'

at the end of the routes.rb file. This is important, since the routes are stepped through top down.

在 routes.rb 文件的末尾。这很重要,因为路由是自上而下的。

See also http://guides.rubyonrails.org/routing.html-> 3.10

另见http://guides.rubyonrails.org/routing.html-> 3.10

回答by steel

For Rail 4, you need to specify the request type:

对于 Rail 4,您需要指定请求类型:

match "*path", to: "application#custom_action", via: :all

As others have said, put this at the very bottom of your routes file.

正如其他人所说,将其放在路由文件的最底部。

回答by prograils

It is not mandatory to use exactly "path" in the match '*path'statement. You can set a whatever token there:

match '*path'语句中完全使用“路径”不是强制性的。您可以在那里设置任何令牌:

get "*string1"

or

或者

get "*string2"

Rails will assign your real HTTP-query to the param named after your token, example:

Rails 会将您的真实 HTTP 查询分配给以您的令牌命名的参数,例如:

get "*user" => "users#show"

in console:

在控制台中:

Started GET "/john" ....  
Processing by UsersController#show as HTML 
Parameters: {"user"=>"john"}

You can use more than one asterisks, say get "*id*user". But in this case you will get some unpredictable result, because Rails processes 2 or more asterisks "in an intuitive way" - for more info see http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

您可以使用多个星号,例如get "*id*user"。但在这种情况下,您会得到一些不可预测的结果,因为 Rails 以“直观的方式”处理 2 个或更多星号 - 有关更多信息,请参见http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard -段

回答by Paul Pettengill

In addition to @steel and @awenkhh, I recommend adding the following to that route's controller action

除了@steel 和@awenkhh,我建议将以下内容添加到该路由的控制器操作中

respond_to do |format|
  format.html
  # other formats you already support
  format.all { render text: '' }
end

Otherwise, you'll wind up with some ActionView::MissingTemplate: Missing templateerrors for formats that you weren't expecting.

否则,您最终会遇到一些ActionView::MissingTemplate: Missing template意想不到的格式错误。

[rant]Particularly helpful for those people trying erroneous attack vectors around /wp-admin/css/wp-admin.cssand the like. I seem to get about 100 requests for /wp-admin/*a day, from super annoying people who apparently would like me to get a more expensive Rollbar account plan.[/rant]

[rant]对那些尝试错误的攻击向量/wp-admin/css/wp-admin.css等的人特别有帮助。我似乎/wp-admin/*每天收到大约 100 个请求,来自超级讨厌的人,他们显然希望我获得更昂贵的 Rollbar 帐户计划。[/rant]