为什么在Rails中进行路由时使用match而不是get? [英] Why use match rather than get when routing in Rails?

查看:97
本文介绍了为什么在Rails中进行路由时使用match而不是get?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby on Rails 3教程中,代码使用:

In the Ruby on Rails 3 tutorial, the code uses:

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'

而不是

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'

,即使所有路由都只需要HTTP GET动词。为什么不使用 get (或 match <上的:via => [:get] )并根据实际情况限制路由操作?

even though all the routes only want the HTTP GET verb. Why not use get (or :via => [:get] on match) and limit the routing action as a matter of practice?

推荐答案

我认为这是最佳做法使用 get [...] 代替 match 。正如您已经正确提到的, match 将同时创建GET和POST路由。如果不需要它们,为什么要创建它们呢?

I would consider it a best practice to use get [...] instead of match. As you already mentioned correctly, match will create both GET and POST routes. Why create them if you do not need them?

使用正确的匹配器(获取或发布)可以使路由保持整洁,并有助于防止应用程序发生不良行为。后一点尤其适用于POST路由,在POST路由中,您不希望在您的网页上意外放置一个GET请求链接,但搜索机器人可能会随后跟随它。

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.

Update [ 2013-05-12]:
从Rails 4.0开始,您现在需要明确指定请求方法

Update [2013-05-12]: From Rails 4.0 onwards you are now required to explicitly specifiy the request method.

这篇关于为什么在Rails中进行路由时使用match而不是get?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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