问题
I have been reading up on the difference between put and post requests and I have some related questions as it pertains to rails: I would like to change one specific field in an already created row...should I use a put or a post request? For example are the following different?
#Assume this is a put request
def update
@model=Model.find(x)
@model.field="new_field"
@model.save
end
#Assume this is a post request
def update
@model=Model.find(x)
@model.field="new_field"
@model.save
end
#What if I use the rails update method?
def update
@model=Model.find(x)
@model.update(model_params)
@model.save
end
Thanks in advance.
回答1:
According to rails convention,
PUT is used for updating an existing resource
POST is used for creating a new resource
In rails 4, PUT has been changed to PATCH to avoid confusion.
Rails generated routes will look like below by default
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
Notice the action for PUT and POST
回答2:
Rails by default aims to use HTTP verbs in the manner laid out by the REST specification, you should not be concerned as to why the methods may allow you to carry out the same action. Instead you should think about providing an API that is RESTful and that users will understand. These default behaviour can be overridden.
REST denotes that:
A request using the POST method should act upon the resource collection; adding a new resource to the collection Example URL: http://example.com/resources
A request using the PUT HTTP verb should act upon a single resource within the collection; replacing the resource wholly upon the server Example URL: http://example.com/resource/1
A request using the PATCH HTTP verb should act upon a single resource within the collection; updating certain attributes upon the resource where it stands Example URL: http://example.com/resource/1
Rails 4 now makes use of the PATCH verb over the PUT verb for updating a resource.
回答3:
- I think we should use PATCH when updating some attributes of record
- PUT really means something among context of "replacing" the resource or all his attributes but could also means creating an resource (I'm basing this on what I remember from reading this book: REST API Design Rulebook ), For example when you are moving(copy) AWS S3 resource you are triggering PUT not POST. So yeah PUT is confusing.
- POST should be used when submitting new resource
There is lot of confusion around PATCH as well, I personally agree how JSON API standard is proposing to do it http://jsonapi.org/format/#crud-updating:
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "To TDD or Not"
}
}
}
I love Rails I but truth is it's not entirely following some core Web conventions. Rails is trying to be productive and too strict conventions are holding down the productivity. So don't go overboard when seeking answer for this. Truth is that Rails is treating PUT and PATCH the same way, and apparently both are wrong. So I recommend:
- POST on create
- PATCH on update
- GET on index, new, show
- don't use PUT at all
- if you find yourself that your controller is doing some weird action, try to restructure your controllers (introduce new controller maybe http://jeromedalbert.com/how-dhh-organizes-his-rails-controllers/)
But if your entire project is using PUT everywhere, you don't need to go through and change everything. Just stick to one or the other (PUT or the PATCH).
UPDATE
I've wrote 2 articles on this topic where I go in depth of this topic.
- http://www.eq8.eu/blogs/36-patch-vs-put-and-the-patch-json-syntax-war
- http://www.eq8.eu/blogs/37-post-create-and-put-updatepost
回答4:
PUT and POST are HTTP methods.
In the routes.rb you have to map method and controller#action. In your class you define 3 times the same method. So if you want map these actions to a HTTP method you can't.
You going to change the name of each method and change the implementation to the model class.
来源:https://stackoverflow.com/questions/19504434/rails-put-vs-post