问题
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 objectPUT
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