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 )?
PUT is a method of modifying resource where the client sends data that updates the entire resource . PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.
#ruby #rails. Monkey patching is one of Ruby's most powerful features. It allows programmers to add methods to core classes which can result in some very elegant APIs. However it's quite easy to shoot yourself in the foot if you don't know what you're doing.
In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.
The HTTP PATCH request method applies partial modifications to a resource. PATCH is somewhat analogous to the "update" concept found in CRUD (in general, HTTP is different than CRUD, and the two should not be confused). A PATCH request is considered a set of instructions on how to modify a resource.
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 objectPUT
is a standard way when you like fully overwrite your object with
new dataWhile 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With