你如何处理“行动”? RESTful 设计中的资源,例如标记电子邮件已读?

发布于 2024-07-13 00:09:47 字数 151 浏览 3 评论 0原文

您如何将此类内容集成到 RESTful 设计中?

  • 将电子邮件标记为已读
  • 对故事进行投票
  • 将消息报告为不适当

此外,您将如何做到这一点,即无需太多魔法就可以创建一个小图标链接或按钮来执行操作?

How would you integrate things like this in a RESTful design?

  • Marking an email as read
  • Voting on a story
  • Reporting a message as inappropriate

Additionally, how would you do it in such a way that one could make a small icon link or button to do the action without too much wizardry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-07-20 00:09:47

这取决于你在做什么。 有时,只需将您的观点从“对资源执行操作”转变为“创建另一个不同但相关的资源”。

对故事进行投票很简单:您可以创建、审查等投票资源。报告也是如此。 这可以应用于任何事物,而不是将订单标记为已提交,而是有一个提交等。关键是弄清楚如何将动词转换为名词。

将电子邮件标记为已读有点不同。 为此,我要做的是使用虚拟属性:在模型上创建自己的 setter 方法(例如,mark_as_read=)将允许您在 #Update 操作中通过 params 数组传递 mark_as_read。

以对一个故事进行投票为例。 您要做的就是创建一个属于故事的投票模型。 创建一个 Votes 控制器,并将其嵌套在路由文件中的 Stories 控制器之外。 然后,您可以使用嵌套资源路由(传递故事记录)轻松创建针对单个故事的投票。

请注意,资源不一定必须由数据库模型支持,尽管它是正确的形式。 在这种情况下,您始终可以简单地修改故事记录,同时保持 REST 性以及在需要时轻松扩展为完整模型的能力。

That depends on what you're doing. Sometimes all it takes is shifting your viewpoint from "performing an action on a resource" to "creating another, different, but related resource."

Voting on a story is easy: a Vote resource that you can create, review, etc. Same thing with a Report. This can be applied to anything, instead of marking an Order as submitted, it instead would have a Submission, etc. The key is to figure out how to transfigure your verb into a noun.

Marking an email as read is a bit different. What I would do for that is to use a virtual attribute: creating your own setter methods on your model (mark_as_read=, for instance) will let you pass mark_as_read through the params array in an #Update action.

To take the example of voting on a story. What you would do is create a Vote model, which belongs to a Story. Create a Votes controller, and have it nested off the Stories controller in your routes file. then, you can use your nested resource routes (passing in your Story record) to easily create votes scoped to the individual story.

Note that the resource doesn't necessarily have to be backed by a database model, although it would be proper form. You could always simply modify the Story record, in this case, while maintaining the RESTfulness and the ability to easily expand to a full model if needed.

咽泪装欢 2024-07-20 00:09:47

这是任何 REST 不可或缺的资源:

http://oreilly.com/catalog/9780596529260/ 宁静的网络服务

This is an indispensable resource for anything REST:

http://oreilly.com/catalog/9780596529260/restful web services

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文