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 i
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.
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.
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
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:
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.