Ruby on Rails 3 文档:“我可以吗” \“可取吗”使用(至少)嵌套资源?
我阅读了 Rails Routing from the Outside In,特别是 2.7 嵌套资源 \ 2.7.1 嵌套限制
资源永远不应该嵌套更多 深度超过 1 层。
这是什么意思?也就是说,“我可以”\“是否建议”使用这样的一级嵌套资源
namespace "users" do
resources :publishers do
resources :magazines
end
end
或者我应该使用这样的东西
namespace "users" do
resources :publishers
resources :magazines
end
? 你觉得怎么样关于?
如果这是推荐的方法,如何编写路由路径(例如 new_users_publisher_magazine
...)?
I read Rails Routing from the Outside In, in particular the section 2.7 Nested Resources \ 2.7.1 Limits to Nesting where it says
Resources should never be nested more
than 1 level deep.
What does it mean? That is, "can I" \ "is it advisable" use one level nested resource like this
namespace "users" do
resources :publishers do
resources :magazines
end
end
or I should use something like this
namespace "users" do
resources :publishers
resources :magazines
end
? What do you think about?
If it is a recommended approach, how to write route paths (for example new_users_publisher_magazine
...)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您也可以对资源使用浅层路由。这是两全其美的做法。即使您在代码中将资源嵌套多层,它也会自动将资源嵌套一层。
You can use shallow routes for resources as well. This follows the best of both worlds. It automatically nests resources one level deep even if you nest them multiple levels in your code.
这取决于你需要什么。
会给你不同的路线
一方面,第二个给你像
/users/1/publishers/ 和 /users/1/magazines
这样的路线,而第一个给你
/users/1/publishers/1/magazines
建议不要尽可能地将这么多内容嵌入到您的路线中,除非您真的非常需要,就像您的生活依赖于它一样:P。不过,这正是我的想法,因为三层的巢穴可能会给你带来更多的痛苦,而不是对你的帮助。
It depends on what you need.
will give you different routes than
For one thing, the second one gives you routes like
/users/1/publishers/ and /users/1/magazines
while the first one gives
/users/1/publishers/1/magazines
It is recommended not to nest that much into your routes as much as possible unless you REALLY REALLY need to, like your life depended on it :P. That's just what i think though, because a nest of 3 levels will probably make you suffer more than it will help you.
我认为这是品味问题。这只是一个指南,而不是规则。如果您对第一种方法感觉更好,那就继续吧。我使用双重嵌套资源,我的同事中没有人不抱怨它。但当然,如果我看到 4 层或更多层的嵌套资源,就很难编写路径,所以对我来说限制是两层(有时是 3 层)。您应该选择自己的限制。
I think it's matter of taste. It's just a guide not a rule. If you feel better with first approach then go for it. I use double nested resources and nobody from my coworkers doesn't complaint about it. But of course If I would see 4 or more levels of nested resources it would be just hard to write paths, so limit for me is two (sometimes 3). You should choose your own limit.
它说不止一个级别,所以这是正确的,并且推荐,因为您将始终通过出版商访问杂志:
It says more than one level so this is right and recommended as far as you will always will access a magazine through a publisher:
这取决于模型:如果一本杂志属于出版商,而出版商属于用户,那么两级嵌套就有意义(尽管实际上并不是必要的)。如果发布商有很多用户,那么我建议不要采用这种方法,因为它会使事情变得不必要的复杂化。
要查看资源的命名路由助手,您可以使用“rake paths”。它很好地概述了已定义的路线。
It depends on the models: if a magazine belongs to a publisher and a publisher belongs to a user, then the two-level nesting would make sense (though it's not really necessary). If a publisher has many users, then I would advise against this approach since it will make things unnecessarily complicated.
To view the named route helpers for your resources you can use 'rake routes'. It gives a nice overview of the defined routes.