Are deeply nested polymorphic resources worth the effort?
Question
I am at the point in my development that I am thinking that deeply (>1) nested resources are not worth the effort.
I have something like this:
resources :first-level do
resources :comments
resources :second-level do
resources :comments
resources :first-child do
resources :comments
end
resources :second-child do
resources :comments
end
resources :third-child do
resources :comments
end
end
end
The kicker is that the comments are polymorphic to the other resources. My intent was to have clean looking URLs like ~/first-level/34/comments, ~/first-level/34/second-level/56/third-level/comments etc.
The problem so far is the polymorphic routes when nested cause nothing but grief. I am following a couple of Ryan Bates Railscasts as an example. For example if I try to use polymorphic_path on the first level it works fine and I get:
polymorphic_path([@commentable, comments]) => ~/first-level/34/comments
but the same code on ~/first-level/34/second-level/23
fails with:
undefined method 'second-level_comment_path' for #<#<Class:0x007fcc4acfbe58>:0x007fcc4ae73d08>
but when I look at my routes the actual named route is first-level_second-level_comment
. I tried to manually create that second-level_comment_path
to basically alias to first-level_second-level_comment
but I could not seem to make that work either.
Unless someone can point out an obvious error here I am leaning towards this approach (http://weblog.jamisbuck.org/2007/2/5/nesting-resources) and just un-nesting these. I have a breadcrumb style navigation to show the hiearchy so that should suffice and the more I look at it the urls do get a bit unwieldily.
Solution
With nested resources, you will need to specify all the parent levels when you want to access a child level. Otherwise Rails will not know how to get to your child level. So, you will need to use first-level_second-level_comment and supply first-level and second-level values like this:
first-level_second-level_comments_path(@my_first_level, @my_second_level)
will render:
~/first-level/34/second-level/23/comments
EDIT:
I don't see why you will need to build path incrementally.
You can always build first_level comments path:
first-level_comments_path(@my_first_level)
will render
~/first-level/34/comments
Or listing of all second levels within the first level: (second-level's index action for a given first-level)
first-level_second-levels_path(@my_first_level)
will render
~/first-level/34/second-levels