Ruby-on-rails Rails POST、PUT、GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/778385/
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:10:42  来源:igfitidea点击:

Rails POST, PUT, GET

ruby-on-railsrubyweb-serviceshttp

提问by geowa4

After I generate a scaffold, Rails gives me the ability to POST to items.xmlwhich will create a new item. A GET to items.xmlwill simply list them all. Where does Rails specify which method in the controller (createor index, respectively) will be called, based on the type of action I am performing?

在我生成一个脚手架后,Rails 使我能够 POST 到items.xml它将创建一个新的item. GETitems.xml将简单地列出所有这些。根据我正在执行的操作类型,Rails 在哪里指定将调用控制器中的哪个方法(createindex,分别为 )?

More specifically, POST calls methodA but GET to the same URL calls methodB. Where is this specified? Where does Rails make the determination to call the indexmethod of the controller?

更具体地说,POST 调用methodA,但GET 到相同的URL 调用methodB。这是在哪里规定的?Rails 在哪里决定调用index控制器的方法?

回答by Matt Grande

I believe it's specified by REST. Here's a list for ya:

我相信它是由REST指定的。这是一份给你的清单:

GET    /items        #=> index
GET    /items/1      #=> show
GET    /items/new    #=> new
GET    /items/1/edit #=> edit
PUT    /items/1      #=> update
POST   /items        #=> create
DELETE /items/1      #=> destroy

Edited to addto get all those routes, in config/routes.rb, simply add map.resources :items

编辑添加以获取所有这些路由,在 config/routes.rb 中,只需添加map.resources :items

回答by John Topley

Rails defines seven controller methods for RESTful resources by convention. They are:

按照惯例,Rails 为 RESTful 资源定义了七个控制器方法。他们是:

Action   HTTP Method  Purpose
-------------------------------------------------------------------------
index    GET          Displays a collection of resources
show     GET          Displays a single resource
new      GET          Displays a form for creating a new resource
create   POST         Creates a new resource (new submits to this)
edit     GET          Displays a form for editing an existing resource
update   PUT          Updates an existing resource (edit submits to this)
destroy  DELETE       Destroys a single resource

Note that because web browsers generally only support GET and POST, Rails uses a hidden field to turn these into PUT and DELETE requests as appropriate.

请注意,由于 Web 浏览器通常仅支持 GET 和 POST,因此 Rails 使用隐藏字段将它们适当地转换为 PUT 和 DELETE 请求。

Specifying map.resources :itemsin config/routes.rbgets you those seven methods "for free". You can list all the routes within your application at any time by entering rake routesin the console.

指定map.resources :itemsinconfig/routes.rb可以“免费”获得这七种方法。您可以随时通过rake routes在控制台中输入来列出应用程序中的所有路由。

回答by Don Werve

The best place to learn about this would be the Routing Guide.

了解这方面的最佳位置是Routing Guide

回答by buddhamagnet

Did you want to know how to use POST only? Do this, for example:

您想知道如何仅使用 POST 吗?这样做,例如:

resources :items, :only => [:create]

..etc. This is for Rails 3 by the way, and will generate a single resource to POST create. Or if you only need a really small subset of the REST set, just:

..等等。顺便说一下,这是针对 Rails 3 的,它将生成一个单独的资源以进行 POST 创建。或者,如果您只需要 REST 集的一个非常小的子集,只需:

match 'items/:id' => "items#create', :via => :post

etc etc.

等等等等

回答by vrish88

Like Don Werve said, take a look at your routes.rb file. In there you probably have something like this:

就像 Don Werve 所说的,看看你的 routes.rb 文件。在那里你可能有这样的事情:

map.resources :items

This is where rails links the POST and GET requests to certain actions. To see how this works look at the links from the other answers. The docs help a ton.

这是 rails 将 POST 和 GET 请求链接到某些操作的地方。要了解这是如何工作的,请查看其他答案中的链接。文档有很大帮助。

To all the routes and which actions they link to you can type rake routesinto the command prompt when you are in the root of your rails directory. This will show you everything (in terms of routing) that a scaffold gives you.

rake routes当您位于 rails 目录的根目录时,您可以在命令提示符中键入所有路由以及它们链接到的操作。这将向您展示脚手架为您提供的所有内容(在路由方面)。

回答by jshen

This will help a lot, but it's not a direct answer to your question. The following command will list the mappings your app uses so you don't have to remember all the details or guess.

这将有很大帮助,但它不是您问题的直接答案。以下命令将列出您的应用程序使用的映射,因此您不必记住所有细节或猜测。

$ rake routes

To answer more directly, this is a convention that rails uses. You set this mapping up when you put something like the following in your routes.rb

为了更直接地回答,这是 rails 使用的约定。当您在 routes.rb 中放置类似以下内容时,您可以设置此映射

map.resources :items

回答by srboisvert

map.resources is a method that automagically gives you the REST routes and path helpers as well. This is a nice feature if you already know and understand how rails' restful routing works but it is also a bit of a hindrance for learning rails because too much is hidden.

map.resources 是一种自动为您提供 REST 路由和路径助手的方法。如果您已经知道并理解 rails 的 restful 路由是如何工作的,这是一个很好的功能,但它也是学习 rails 的一个障碍,因为隐藏了太多。

Railsguideshas a nice routes guide.

Railsguides有一个不错的路线指南

回答by Michael

To be honest, you can't really go wrong with the routing documentation on the Rails website. This has helped take the next steps and move beyond the comfort of resources (which for most apps is fine)and really nail down the solid routing features available.

老实说,Rails 网站上的路由文档绝对不会出错。这有助于采取后续步骤并超越资源的舒适度(这对于大多数应用程序来说很好),并真正确定了可用的可靠路由功能。

http://guides.rubyonrails.org/routing.html

http://guides.rubyonrails.org/routing.html