简体   繁体   English

Rails POST,PUT,GET

[英]Rails POST, PUT, GET

After I generate a scaffold, Rails gives me the ability to POST to items.xml which will create a new item . 在我生成一个脚手架后,Rails让我能够POST到items.xml ,这将创建一个新item A GET to items.xml will simply list them all. items.xml的GET将简单地列出它们。 Where does Rails specify which method in the controller ( create or index , respectively) will be called, based on the type of action I am performing? 根据我正在执行的操作类型,Rails在哪里指定控制器中的哪个方法(分别是createindex )?

More specifically, POST calls methodA but GET to the same URL calls methodB. 更具体地说,POST调用methodA但是GET到同一个URL调用methodB。 Where is this specified? 这指定在哪里? Where does Rails make the determination to call the index method of the controller? Rails在哪里决定调用控制器的index方法?

I believe it's specified by REST . 我相信它是由REST指定的。 Here's a list for ya: 这是ya的列表:

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 add to get all those routes, in config/routes.rb, simply add map.resources :items 编辑添加以获取所有这些路由,在config / routes.rb中,只需添加map.resources :items

Rails defines seven controller methods for RESTful resources by convention. Rails按惯例为RESTful资源定义了七种控制器方法。 They are: 他们是:

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 :items in config/routes.rb gets you those seven methods "for free". 指定map.resources :items config/routes.rb map.resources :items可以“免费”获取这七种方法。 You can list all the routes within your application at any time by entering rake routes in the console. 您可以通过在控制台中输入rake routes随时列出应用程序中的所有rake routes

了解这一点的最佳位置是路由指南

Did you want to know how to use POST only? 您想知道如何仅使用POST吗? Do this, for example: 这样做,例如:

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

..etc. ..等等。 This is for Rails 3 by the way, and will generate a single resource to POST create. 顺便说一下,这适用于Rails 3,并将为POST创建一个单独的资源。 Or if you only need a really small subset of the REST set, just: 或者如果您只需要一小部分REST集,只需:

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

etc etc. 等等

Like Don Werve said, take a look at your routes.rb file. 就像Don Werve所说,看一下你的routes.rb文件。 In there you probably have something like this: 在那里你可能有这样的事情:

map.resources :items

This is where rails links the POST and GET requests to certain actions. 这是rails将POST和GET请求链接到某些操作的位置。 To see how this works look at the links from the other answers. 要了解其工作原理,请查看其他答案中的链接。 The docs help a ton. 文档帮助了很多。

To all the routes and which actions they link to you can type rake routes into the command prompt when you are in the root of your rails directory. 对于所有路由以及它们链接到哪些操作,当您位于rails目录的根目录中时,可以在命令提示符中键入rake routes This will show you everything (in terms of routing) that a scaffold gives you. 这将显示脚手架为您提供的所有内容(就路由而言)。

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. 为了更直接地回答,这是rails使用的约定。 You set this mapping up when you put something like the following in your routes.rb 在routes.rb中放入类似下面的内容时,可以设置此映射

map.resources :items

map.resources is a method that automagically gives you the REST routes and path helpers as well. map.resources是一种自动为您提供REST路由和路径助手的方法。 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. 如果您已经了解并理解rails的restful路由是如何工作的,那么这是一个很好的功能,但它也是学习rails的一个障碍,因为隐藏的太多了。

Railsguides has a nice routes guide . Railsguides有一个很好的路线指南

To be honest, you can't really go wrong with the routing documentation on the Rails website. 说实话,Rails网站上的路由文档确实不会出错。 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. 这有助于采取下一步措施,超越资源的舒适度(大多数应用程序都很好),并确实确定了可用的可靠路由功能。

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

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

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