【问题标题】:Chapter 5 of Hartl book Why does routing requires get() for user.new?Hartl book 的第 5 章Why does routing requires get() for user.new?
【发布时间】:2011-09-23 17:25:38
【问题描述】:

在 Hartl rails 教程 (http://ruby.railstutorial.org/) 的清单 5.29 附近,讨论了一个路由错误,它迫使您在 users/new 上调用 get 方法,而不仅仅是 match 方法。稍后通过调用下一章中的资源方法来纠正这一点。

我的一般问题是为什么我们不必在以下列表中的 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

【问题讨论】:

    标签: ruby-on-rails rest routing


    【解决方案1】:

    不同之处在于路由将匹配的 HTTP 请求类型。

    通过使用get "users/new",路由将只匹配 HTTP GET 请求。

    match "users/new" 实际上会匹配所有类型的 HTTP 请求。

    如果您只期望对它们的 GET 请求(似乎是这种情况),您可以对其他路由使用 get 而不是 match。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2013-02-25
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      相关资源
      最近更新 更多