对此最好的 Rails 约定是什么?
假设您有一个资源,它是完全在另一个资源(例如评论或标签)的视图中创建和显示的。您是否仍然应该将其设为自己的资源,或者将其设为嵌套资源会是更好的主意吗?另外,您应该使用虚拟属性吗?
我知道这个问题很普遍,但我并不是在寻找具体的答案,只是对何时首选每种技术进行一般性解释。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些一般性的想法:
Some general thoughts:
一般来说,将其设为嵌套资源会更有用。这是更“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.