我想在我的 rails 用户模型中添加一个新的 update(new_update) 操作,它将更新模型中的单个列.我应该在路由文件中使用哪个 rest api 方法.我应该使用 put 或 patch 还是两者都使用.
I want to add a new update(new_update) action in my user model of rails which will update a single column in the model. which rest api method I should use in routes file. Should I use put or patch or both.
resources: users do member do put 'new_update' patch 'new_update' end 推荐答案如果你想 100% 匹配 HTTP 动词定义.
If you want to match 100% with the HTTP verb definition.
PUT 应该用请求中的内容完全覆盖您的目标资源(请求中不存在的所有字段都应设置为已删除/无效).
PUT is supposed to overwrite your targeted resource entirely with the content in the request(all field non present in the request should be set to removed/nullified).
PATCH 应该只修改请求中发送的字段.
PATCH is supposed to only modify the fields sent in the request.
话虽这么说,但大多数时候人们并没有区别并使用它们中的任何一个(很多人甚至不知道有 PATCH 动词)是使用具有与 PATCH 相同行为的 PUT(因为 PUT 的用例在 imo 上非常罕见).
That being said, most of the time people don't make the difference and use either of them (a lot of people don't even know that there is a PATCH verb) is use PUT with the same behavior as PATCH (since the use case for PUT is quite rare on imo).
更多推荐
放置或修补新的更新操作 Rails
发布评论