Ruby on rails 每个所属的有多个关联需要嵌套路由吗?

Ruby on rails 每个所属的有多个关联需要嵌套路由吗?,ruby-on-rails,ruby-on-rails-3,routes,associations,Ruby On Rails,Ruby On Rails 3,Routes,Associations,这是一个相当基本的问题,但我一直无法在网上找到具体的答案。每个有多个所属的不需要嵌套路由正确吗?在查找class/:id/class/:id格式的URL时,是否应该只使用嵌套路由 例如,假设我有两个类Profile和Post 模型/简介 has_many :posts 模特/职位 belongs_to :profile 没有单独的帖子URL,帖子显示在profiles/show中。在这种情况下,post路由是否应该只在:profiles资源中嵌套:new、:create和:destroy之类

这是一个相当基本的问题,但我一直无法在网上找到具体的答案。每个有多个所属的不需要嵌套路由正确吗?在查找class/:id/class/:id格式的URL时,是否应该只使用嵌套路由

例如,假设我有两个类Profile和Post

模型/简介

has_many :posts
模特/职位

belongs_to :profile
没有单独的帖子URL,帖子显示在profiles/show中。在这种情况下,post路由是否应该只在:profiles资源中嵌套:new、:create和:destroy之类的操作?《rails指南》指出,资源的嵌套深度不应超过一层,而且经常会出现这种情况。为每个关联生成嵌套资源似乎很快就会违反此规则。提前谢谢

如果不使用/profile/1/posts或/profile/1/posts/1,则不需要嵌套路由。然而,我敦促您重新考虑,嵌套路由有助于实现干净的RESTful API

例如,整洁的小型嵌套管线:

resources :profile, :shallow => true do
  resources :posts
end
将为您提供所有这些真正有用的路线:

profile_posts GET /profile/:profile_id/posts(.:format) posts#index POST /profile/:profile_id/posts(.:format) posts#create new_profile_post GET /profile/:profile_id/posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy profile_index GET /profile(.:format) profile#index POST /profile(.:format) profile#create new_profile GET /profile/new(.:format) profile#new edit_profile GET /profile/:id/edit(.:format) profile#edit profile GET /profile/:id(.:format) profile#show PUT /profile/:id(.:format) profile#update DELETE /profile/:id(.:format) profile#destroy 如果不需要使用/profile/1/posts或/profile/1/posts/1,则不需要嵌套管线,请使用不需要嵌套管线的浅管线。然而,我敦促您重新考虑,嵌套路由有助于实现干净的RESTful API

例如,整洁的小型嵌套管线:

resources :profile, :shallow => true do
  resources :posts
end
将为您提供所有这些真正有用的路线:

profile_posts GET /profile/:profile_id/posts(.:format) posts#index POST /profile/:profile_id/posts(.:format) posts#create new_profile_post GET /profile/:profile_id/posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy profile_index GET /profile(.:format) profile#index POST /profile(.:format) profile#create new_profile GET /profile/new(.:format) profile#new edit_profile GET /profile/:id/edit(.:format) profile#edit profile GET /profile/:id(.:format) profile#show PUT /profile/:id(.:format) profile#update DELETE /profile/:id(.:format) profile#destroy
如果您阅读了Ruby on Rails指南的第2.7节,其中说明:

嵌套路由允许您在路由中捕获此关系

请参阅-以供参考

除此之外,您可能希望在类上执行特定操作,即具有创建、编辑等功能的用户。。。每个用户都与特定的预订相关联。这意味着,无论何时你对用户做任何事情,你实际上是在对用户/预订做一些事情。因为这是与此相关联的


RESTful路由是设置应用程序和充分利用统一资源标识符的一种干净方法。例如,识别一个特定的用户,例如/users/1/bookings/3,这将显示第一个用户

如果您阅读RubyonRails指南的第2.7节,它会指出:

嵌套路由允许您在路由中捕获此关系

请参阅-以供参考

除此之外,您可能希望在类上执行特定操作,即具有创建、编辑等功能的用户。。。每个用户都与特定的预订相关联。这意味着,无论何时你对用户做任何事情,你实际上是在对用户/预订做一些事情。因为这是与此相关联的


RESTful路由是设置应用程序和充分利用统一资源标识符的一种干净方法。例如,识别一个特定的用户,例如/users/1/bookings/3,这将显示第一个用户

所以,最好的做法是将帖子作为一个嵌套的配置文件资源?我确实这么认为。请重读答案,我已经添加了更多信息。特别是关于:shallow=>true。但最终这取决于你想要什么。如果你百分之百确定你永远不想使用浅路由,那么当然,把它们排除在外。因此,最好的做法是让帖子成为配置文件的嵌套资源?我确实这么认为。请重读答案,我已经添加了更多信息。特别是关于:shallow=>true。但最终这取决于你想要什么。如果你100%确信你永远不想使用浅路线,那么当然,把它们排除在外。