您可以将路线嵌套到您喜欢的深度,因为 rails 3.x 允许您使用 shallow: true 将它们展平
尝试尝试
resources :account, shallow: true do
resources :people do
resources :notes
end
end
使用 rake 路线看看你得到了什么:)
更新以回应评论
正如我所说,玩 rake routes 看看你能得到什么 url
resources :account, shallow: true do
resources :people, shallow: true do
resources :notes
end
end
为您提供这些路线
:~/Development/rails/routing_test$ rake routes
person_notes GET /people/:person_id/notes(.:format) notes#index
POST /people/:person_id/notes(.:format) notes#create
new_person_note GET /people/:person_id/notes/new(.:format) notes#new
edit_note GET /notes/:id/edit(.:format) notes#edit
note GET /notes/:id(.:format) notes#show
PUT /notes/:id(.:format) notes#update
DELETE /notes/:id(.:format) notes#destroy
account_people GET /account/:account_id/people(.:format) people#index
POST /account/:account_id/people(.:format) people#create
new_account_person GET /account/:account_id/people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
account_index GET /account(.:format) account#index
POST /account(.:format) account#create
new_account GET /account/new(.:format) account#new
edit_account GET /account/:id/edit(.:format) account#edit
account GET /account/:id(.:format) account#show
PUT /account/:id(.:format) account#update
DELETE /account/:id(.:format) account#destroy
可以看出,您可以访问您决定需要的任何级别的所有模型。其余的取决于您在控制器操作中放入的任何内容。
您确实必须执行操作以确保在未传入 id 参数时采取适当的操作,因此如果您为特定模型使用 id,请检查该 id 是否在参数列表中,如果没有则采取替代行动。例如,如果您没有传递帐户 ID,请确保您不要尝试使用它
您的评论表明您已经使用浅层路线,但这不是您在问题中发布的内容?