RESTful 合并资源的方式 [英] RESTful way to merge resources

查看:36
本文介绍了RESTful 合并资源的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个资源

  • http://example.com/places/1
  • http://example.com/places/2

端点应该如何将两个资源合并为一个资源?理想情况下,合并后只剩下 1 个资源.

How should the endpoint look like to merge both resources into a single resource? Ideally after the merge only 1 resource will remain.

乍一看,我可以使用 params[:src_id] = 2 对 http://example.com/places/1 执行 PUT 代码>.这会将第二个资源的属性合并到第一个资源中并删除第二个资源.还有其他人有更好的方法吗?

At first glance I could do a PUT to http://example.com/places/1 with params[:src_id] = 2. Which would merge the attributes from the second resource into the first and delete the the second resource. Does anyone else have a better approach?

请注意,我使用的 Rails 不支持 http MERGE 动词,所以这是不可能的.

Note that I'm using Rails which doesn't support the http MERGE verb so that's out of the question.

推荐答案

我建议一个新的控制器 PlacesMergeController 和一个 new 和 create 动作.在新操作中,您将有一个表单询问要合并的位置.该表单将对创建操作执行 POST 请求,您将在该操作中进行实际合并.根据逻辑的复杂性,您还可以使用 PlacesMerge 模型(非 AR!).

I suggest a new controller PlacesMergeController with a new and create action. In the new action, you would have a form asking which places to merge. The form will do a POST request to the create action, where you do the actual merging. Depending on the complexity of the logic you could also have a PlacesMerge model (non-AR!).

这种方法应该在处理合并操作的源/目标方面为您提供最大的灵活性 - 并且易于维护.

This approach should give you maximum flexibility in terms of handling source/target of the merge operation - and it's easily maintainable.

这篇关于RESTful 合并资源的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆