对此最好的 Rails 约定是什么?

发布于 2024-10-29 19:31:33 字数 149 浏览 4 评论 0原文

假设您有一个资源,它是完全在另一个资源(例如评论或标签)的视图中创建和显示的。您是否仍然应该将其设为自己的资源,或者将其设为嵌套资源会是更好的主意吗?另外,您应该使用虚拟属性吗?

我知道这个问题很普遍,但我并不是在寻找具体的答案,只是对何时首选每种技术进行一般性解释。

Let's say that you have a resource that is created and displayed entirely within the view of another resource (eg. comments or tags). Should you still make it it's own resource, or would it be a better idea to make it a nested resource? Also, should you make use of virtual attributes?

I know this question is general, but I'm not looking for a specific answer, just a general explanation of when each technique is preferred.

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

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

发布评论

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

评论(2

感受沵的脚步 2024-11-05 19:31:44

一些一般性的想法:

  • 嵌套和独立资源不一定是相互排斥的。
  • “完全在另一个资源的视图中创建和显示”是一个强有力的声明,但最终可能并不真实。例如,标签:您是否想要包含具有特定标签的所有文章的列表。嵌套是没有意义的。
  • 在第二层或第三层以下筑巢将开始变得比其价值更麻烦。有些人会说,即使是第一次嵌套也比它的价值更麻烦。
  • 对于具有单一主导资源的应用程序,添加其他独立资源的成本成比例地更高。小型应用程序可以摆脱大型应用程序无法解决的复杂性。
  • 如果您还没有良好的感觉,请分别编写一些资源。答案最多只能证明观点的合理性,最好通过经验来发展你自己的观点。

Some general thoughts:

  • Nesting and independent resources are not necessarily mutually exclusive.
  • "created and displayed entirely within the view of another resource" is a strong statement that can end up not being true. For example, tags: do you ever want a list of all articles with a certain tag. that would not make sense nested.
  • Nesting below the second or third level will begin to be more trouble then its worth. Some will say that even the first nesting is more trouble then its worth.
  • For an app that has a single dominate resource the cost of adding other independent resources is proportionally higher. Small apps can get away with complexities that large apps cannot.
  • If you don't have a good feel yet write a few resources each way. Answers will at best be justifies opinions it is probably best to develop you own through experience.
蔚蓝源自深海 2024-11-05 19:31:44

一般来说,将其设为嵌套资源会更有用。这是更“RESTful”的方式。它还允许您访问父资源,而无需将其指定为参数。但是,您不想将资源嵌套超过一层,如 Rails 路由指南。

Generally speaking, it would be more useful to make it a nested resource. This is the more "RESTful" way. It will also give you access to the parent resource without having to specify it as a parameter. However, you do not want to nest a resource more than one level deep, as noted in the Rails routing guide.

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