How to route multiple levels of nested resources in Rails 3?
I know I can specify nested resources on routes.rb
开发者_Python百科in Rails 3 this way:
resources :users do
resources :posts
end
However, I would like to add comments
to posts
. What should I write in my routes.rb
file? Is this the correct way? Can I keep nesting them?
resources :users do
resources :posts do
resources :comments
end
end
You can keep nesting the way you have shown and things will work fine. There are quite a few sources that will tell you not to go crazy nesting routes though. Take a look at Rails Best Practices for example (I think the article was created for rails 2 but the principals still apply). Jamis Buck also blogged about this a while ago.
Yes, you can keep nesting and nesting and nesting and so forth.
Yes. What you wrote is the correct way.
I've been interested in this same problem and I think you're suppose to do:
resources :users do
resources :posts
end
resources :posts do
resources :comments
end
Check out the API on Resources.
精彩评论