Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 )?

like image 416
current_user Avatar asked Nov 17 '17 09:11

current_user


People also ask

What is the difference between put and patch in REST API?

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.

What is Patch in Ruby on Rails?

#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.

What is RESTful route in Rails?

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.

What is Patch in API call?

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.


1 Answers

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

like image 101
AntonTkachov Avatar answered Oct 06 '22 23:10

AntonTkachov