Ruby on Rails 3 文档:“我可以吗” \“可取吗”使用(至少)嵌套资源?

发布于 2024-10-17 19:34:56 字数 747 浏览 2 评论 0原文

我阅读了 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 技术交流群。

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

发布评论

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

评论(5

忆伤 2024-10-24 19:34:56

您也可以对资源使用浅层路由。这是两全其美的做法。即使您在代码中将资源嵌套多层,它也会自动将资源嵌套一层。

namespace :users do
  shallow do
    resources :publishers do
      resources :maagazines
    end
  end
end

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.

namespace :users do
  shallow do
    resources :publishers do
      resources :maagazines
    end
  end
end
泪冰清 2024-10-24 19:34:56

这取决于你需要什么。

namespace "users" do
  resources :publishers do
    resources :magazines
  end
end

会给你不同的路线

namespace "users" do
  resources :publishers
  resources :magazines
end

一方面,第二个给你像

/users/1/publishers/ 和 /users/1/magazines

这样的路线,而第一个给你

/users/1/publishers/1/magazines

建议不要尽可能地将这么多内容嵌入到您的路线中,除非您真的非常需要,就像您的生活依赖于它一样:P。不过,这正是我的想法,因为三层的巢穴可能会给你带来更多的痛苦,而不是对你的帮助。

It depends on what you need.

namespace "users" do
  resources :publishers do
    resources :magazines
  end
end

will give you different routes than

namespace "users" do
  resources :publishers
  resources :magazines
end

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.

避讳 2024-10-24 19:34:56

我认为这是品味问题。这只是一个指南,而不是规则。如果您对第一种方法感觉更好,那就继续吧。我使用双重嵌套资源,我的同事中没有人不抱怨它。但当然,如果我看到 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.

负佳期 2024-10-24 19:34:56

它说不止一个级别,所以这是正确的,并且推荐,因为您将始终通过出版商访问杂志:

namespace "users" do
  resources :publishers do
    resources :magazines
  end
end

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:

namespace "users" do
  resources :publishers do
    resources :magazines
  end
end
挽心 2024-10-24 19:34:56

这取决于模型:如果一本杂志属于出版商,而出版商属于用户,那么两级嵌套就有意义(尽管实际上并不是必要的)。如果发布商有很多用户,那么我建议不要采用这种方法,因为它会使事情变得不必要的复杂化。

要查看资源的命名路由助手,您可以使用“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.

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