Rails 中 RESTful 路由的更新操作(PATCH 或 PUT) [英] update action of a RESTful route in Rails (PATCH or PUT)
问题描述
我是 Ruby on Rails 的新手.为什么 Rails 中 RESTful 路由的更新操作映射到两个 HTTP 动词,即 PATCH 和 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
更新资源时调用这两者中的哪个方法(一般CRUD)?
Which Method among the two is called when I update a resource(general CRUD )?
推荐答案
请求类型遵循 HTTP 标准.
It's done to follow HTTP standard for request types.
@Mikhail 如何从概念上提到:
How @Mikhail mentioned, conceptually:
PATCH
是合适的请求类型,当您只想更新部分你的对象PUT
是一种标准方式,当您喜欢完全覆盖您的对象时新数据
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
虽然在 Rails 中,这两个操作都可以通过单个 update
操作轻松完成,区别仅在于传递的 params
,然后 Rails 使两条路线来覆盖标准,但实际上没有必要为此制作 2 个不同的控制器操作.
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.
据我所知,Rails 默认使用 PUT
,但您可以使用其中任何一个.只需遵循提到的概念规则
As I know Rails uses PUT
as default, but you can use any of them. Just follow mentioned conceptual rule
这篇关于Rails 中 RESTful 路由的更新操作(PATCH 或 PUT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!