15

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.

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
kempchee
  • 470
  • 1
  • 4
  • 17
  • Check out [this](http://stackoverflow.com/questions/107390/whats-the-difference-between-a-post-and-a-put-http-request), your question has more to do with http definitions and their intendend usage than rails conventions... – rudolph9 Oct 21 '13 at 21:04

5 Answers5

23

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

usha
  • 28,973
  • 5
  • 72
  • 93
  • 1
    I understand all of that...my confusion comes from the overlapping nature of the two. ie. you can also use a POST request to update. My concern is that if you simply want to update one field, a PUT request will completely replace the existing record, requiring you to specify the values of all the fields you want to keep the same, as opposed to only the one you want to change – kempchee Oct 21 '13 at 21:24
6

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.

Keith Richards
  • 155
  • 2
  • 5
4
  • 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:

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.

equivalent8
  • 13,754
  • 8
  • 81
  • 109
  • a good detailed response, but despite its name the JSON API standard is not really *the* standard right now, even though some of the backers are associated with Rails. A question that tries to examine the current options: http://stackoverflow.com/questions/12806386/standard-json-api-response-format – prusswan Mar 09 '16 at 18:02
  • to be honest I like how company Stormpath is proposing RESTfull JSON API in this talk https://www.youtube.com/watch?v=hdSrT4yjS1g . In my opinion I like it more than JSON API http://jsonapi.org/ . But convention over configuration, I go with JSONAPI.org implementation.If comunity agrees to use something else I go with that but it's better to have half standard than no standard at all :) – equivalent8 Mar 12 '16 at 20:17
  • I've updated the answer with 2 resources of my investigation on this topic. I recommend to read them. – equivalent8 Jul 31 '17 at 17:12
1

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.

gzfrancisco
  • 812
  • 10
  • 14
0

I'm not a Rails guy, but I believe those are same everywhere, so long story short:

What's difference between PUT and POST HTTP methods?

  • You would see url of POST method without parameters, like '/api/example', (because there's nothing to change) you just adding (posting).
  • If your URL contains parameters like '/api/example/:id' - now that's the legit way to use PUT method, because you using url params, therefore you must find some data and change it.

to conclude: If your url is without parameters, use POST, otherwise - PUT (request with PUT method usually contains data within, to compare, in case of DELETE method, url also contains parameters but not data in body.