I have a 2 level nesting objects that i need help with
My routes look like this to normalise the url abit. instead of having a url that looks like this /projects/1/tasks/3/comments/3.
resources :projects do
resources :tasks
end
resources :tasks do
resources :comments
end
Model has the ‘has_many’, belongs_to methods.
I can create comments under each task and display them under the tasks, but on the ‘show’ template of the comments i would like to display a link back to the tasks, which i get an error because the tasks controller is asking for a project_id?
How would this normally done when dealing with 2 level nesting?
I would do
Which is basically what you’re doing except you can’t generate a
projects_taskmember path(ieprojects/1/tasks/1) anymore they’d all just betaskmember paths(ie ‘/tasks/1’).Member paths include
show,update,delete,editBut the
project_taskscollection paths(ieprojects/1/tasks) would still be available.Collection paths include
index,create,newcommentpaths wouldn’t change. Allcommentpaths would still include thetask/:task_idprefix.Checkout the
resourcesdocumentation for more info on that (also more info onmemberandcollectionalso on that page.)But to actually solve your problem
You need to look up the
project_idwhen you link back to theproject_tasksindex. So you would need to doThat way the
Task#indexknows where the parent project is. This is the solution for both implementations.Check out the
UrlFordocumentation for more info on that.If you want access to a
@projectvariable in aCommentviewThen you just need to look up the
projectin the controller instead of at a view level. So basically