简体   繁体   English

为什么在Rails中使用匹配而不是获取?

[英]Why use match rather than get when routing in Rails?

In the Ruby on Rails 3 tutorial, the code uses: 在Ruby on Rails 3教程中,代码使用:

match '/signup',  :to => 'users#new'
match '/signin',  :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'

match '/contact', :to => 'pages#contact'
match '/about',   :to => 'pages#about'
match '/help',    :to => 'pages#help'

rather than 而不是

get '/signup',  :to => 'users#new'
get '/signin',  :to => 'sessions#new'
get '/signout', :to => 'sessions#destroy'

get '/contact', :to => 'pages#contact'
get '/about',   :to => 'pages#about'
get '/help',    :to => 'pages#help'

even though all the routes only want the HTTP GET verb. 即使所有路由只需要HTTP GET动词。 Why not use get (or :via => [:get] on match ) and limit the routing action as a matter of practice? 为什么不在match使用get (或:via => [:get] )并限制路由操作作为实践?

I would consider it a best practice to use get [...] instead of match . 我认为使用get [...]而不是match是最佳做法。 As you already mentioned correctly, match will create both GET and POST routes. 正如您已经正确提到的, match将创建GET和POST路由。 Why create them if you do not need them? 如果您不需要它们,为什么要创建它们?

Using the correct matchers (get or post) keeps your routes clean and helps prevent unwanted behavior of your application. 使用正确的匹配器(获取或发布)可以保持路由清洁,并有助于防止应用程序出现意外行为。 The latter point is true especially for POST routes, where you do not want to accidentially put a GET request link on your webpage that can be followed by search bots. 后一点尤其适用于POST路由,您不希望在您的网页上意外地发出GET请求链接,搜索机器人可以跟随该路径。

Update [2013-05-12]: From Rails 4.0 onwards you are now required to explicitly specifiy the request method . 更新[2013-05-12]:从Rails 4.0开始,您现在需要明确指定请求方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM