简体   繁体   中英

Rails 3: Nested Resources 3 Levels Deep?

I've read this article by James Buck, but it's a bit out of date.

Here's what I know is considered bad practice:

resources :companies do
  resources :users
  resources :roles
  resources :people do
    resources :notes
  end
end

Is this what I should be doing?

resources :companies do
  resources :users, :roles, :people
end

resources :people do
  resources :notes
end

And can someone please explain to me why or why not?

Both will generate the exact routes for :companies , :users , :roles and for :people .

But :notes route will differ .

In first case for the :notes route will generate something like :

/companies/:company_id/people/:person_id/notes(.:format) 
/companies/:company_id/people/:person_id/notes/new(.:format)

and in second case routes will not relate with companies , and looks like :

/people/:person_id/notes(.:format)
/people/:person_id/notes/new(.:format)

So I think its depend on your application and which routes you want to prefer .

Hope this will help .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM