The following post is based on Rails 4.
I'm actually looking for a good best-practices about the multiple nested resources (more than 1), and the option shallow: true.
First in my routes, there was this :
resources :projects do
resources :collections
end
The routes associated are :
project_collections GET /projects/:project_id/collections(.:format) collections#index
POST /projects/:project_id/collections(.:format) collections#create
new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new
edit_project_collection GET /projects/:project_id/collections/:id/edit(.:format) collections#edit
project_collection GET /projects/:project_id/collections/:id(.:format) collections#show
PATCH /projects/:project_id/collections/:id(.:format) collections#update
PUT /projects/:project_id/collections/:id(.:format) collections#update
DELETE /projects/:project_id/collections/:id(.:format) collections#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
I read in the documentation about the limitation of nested resources :
Resources should never be nested more than 1 level deep.
Source : http://guides.rubyonrails.org/routing.html#limits-to-nesting Ok. Then, like the documentation said, I'm gonna use "shallow" in my routes.
shallow do
resources :projects do
resources :collections
end
end
The routes associated are :
project_collections GET /projects/:project_id/collections(.:format) collections#index
POST /projects/:project_id/collections(.:format) collections#create
new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new
edit_collection GET /collections/:id/edit(.:format) collections#edit
collection GET /collections/:id(.:format) collections#show
PATCH /collections/:id(.:format) collections#update
PUT /collections/:id(.:format) collections#update
DELETE /collections/:id(.:format) collections#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
The major difference I see is the "show" of collections, this one :
collection GET /collections/:id(.:format) collections#show
So if I I'm right, the link for the show action for a collection is :
<%= link_to 'Show", collection_path(collection)%>
and should return something like this : "http://example.com/collections/1"
BUT ! 2 things :
- This is not working. I'm getting instead "http://example.com/projects/1". WTF ?
- Even if it was working, it's actually pretty bad because I loose the REST basic that say "Collection is child of project, then the url should be "localhost/project/1/collections/1"
I don't understand what is the interest of shallow if it's to loose the big advantage of Rest actions. What's the interest ? And what is the interest to loose the "Show" action ? I already posted this to SO, but the only comment i got is "It's something normal". WTF? In what this is a normal behavior to "remove" an action from the rest API ?
I reproduced the problem on a neutral project, to be sure that I was not doing something wrong, and the same problem happened. So, yes, it may be convenient for the helpers to use shallow, but it's NOT AT ALL convenient for the rest, you loose all the interest of "one collection is nested to one project, so this is reflected in the URL".
I don't know if there is another way to do this, it's true that shallow allow more flexibility about the helpers, but it's false that it's rest compliant. So, is there any chance to get the "helpers" working (it's pretty awesome to have "nested3_path(collection)" instead of "nested1_nested2_nested3([nested1.nested2.nested3, nested1.nested2, nested1])", and keeping the "url part" and keep having "nested1/123/nested2/456/nested3/789 ?
Thanks !