Hartl 书第 5 章 为什么路由需要 user.new 的 get() ?
Hartl Rails 教程 (http://ruby.railstutorial.org/) 的清单 5.29 周围讨论了一个路由错误,该错误迫使您调用 users/new 上的 get 方法而不仅仅是 match 方法。稍后通过调用下一章中的 resources 方法来纠正此问题。
我的一般问题是为什么我们不必在以下列表中的 PagesController 操作上调用 get() 。
#5.29
SampleApp::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
Around listing 5.29 of the Hartl rails tutorial (http://ruby.railstutorial.org/) there is a discussion of a routing error that forces you to call the get method on users/new and not just a match method. This is later rectified by calling to the resources method in the next chapter.
My general question is why don't we have to call get() on the PagesController actions in the following listing.
#5.29
SampleApp::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
区别在于路由将匹配的 HTTP 请求类型。
通过使用
get "users/new"
路由将仅匹配 HTTP GET 请求。match "users/new"
实际上会匹配所有类型的 HTTP 请求。如果您只期望对其他路由进行 GET 请求(情况似乎如此),则可以对其他路由使用
get
而不是match
。The difference is in the types of HTTP requests the routes will match.
By using
get "users/new"
the route will only match HTTP GET requests.match "users/new"
would actually match all types of HTTP requests.You can use
get
instead ofmatch
for your other routes if you only expect GET requests for them (which appears to be the case).