update action of a RESTful route in Rails (PATCH or PUT)
Asked Answered
A

1

13

I am a newbie to Ruby on Rails.why is the update action of a RESTful route in Rails is mapped to two HTTP verbs i.e, PATCH and PUT?

   PATCH  /articles/:id(.:format)      articles#update
   PUT    /articles/:id(.:format)      articles#update

Which Method among the two is called when I update a resource(general CRUD )?

Ascus answered 17/11, 2017 at 9:29 Comment(1)
Both of these methods can be used for update. But patch is prefered when you want to update partial resource. put - update complete resource representation.Stimson
I
9

It's done to follow HTTP standard for request types.

How @Mikhail mentioned, conceptually:

  • PATCH is a proper request type, when you want to update only part of your object
  • PUT is a standard way when you like fully overwrite your object with new data

While in Rails both of this can be easily done with single update action and the difference is just in passed params, then Rails makes two routes to cover standards, but there is no real need in making 2 different controller action for that.

As I know Rails uses PUT as default, but you can use any of them. Just follow mentioned conceptual rule

Insanitary answered 17/11, 2017 at 10:0 Comment(6)
Tkachov, Thanks for your answer.I dug into this and i am not sure but read PATCH is going to be the primary method for updates in Rails 4.0.Ascus
Maybe you are right. Haven't checked this. It would be great if mark my answer as correct for future searches unless you need anything else in itInsanitary
But PUT doesn't null out missing fields?Macaluso
No, Rails doesn't do anything like that. It would cause a lot of bugs and missunderstandingInsanitary
Can you please share the reference for "Rails uses PUT as default"? It'd be quite helpful to support that. Thanks!Quotha
@ShailendraShukla Hey, sorry. I'm way out of the context of this question as it was 4.5 years agoInsanitary

© 2022 - 2024 — McMap. All rights reserved.