如果你没有用/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
这样你就必须在必要/有用时自由选择嵌套路线,例如
GET /profile/:profile_id/posts/new(.:format) # create a new post for the given profile_id
GET /profile/:profile_id/posts(.:format) # return all posts for the given profile_id
,并使用浅的路线,其中嵌套的路线是没有必要的
因此,最好的做法是使帖子成为配置文件的嵌套资源? – Steve
我确实这么认为。请重新阅读答案,我已经添加了一些更多信息。特别是关于':shallow => true'。但最终它取决于你想要什么。如果你100%肯定你永远不想使用浅层路线,那么当然,就把它们排除在外。 –