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

北城余情 提交于 2020-01-02 08:43:38

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/47347250/update-action-of-a-restful-route-in-rails-patch-or-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!