1

I have looked around for weeks trying to find an example of triple nested forms that include all the components, including how to handle form_for with the deeply nested model. Fairly new to Rails, but would love if someone could show post an example of a triple nested form_for for something like Group->Project->Tasks, showing controller, model and view connection. Having a difficult time with the Tasks form_for. Thanks.

CAKFinn
  • 11
  • 1

1 Answers1

0

Well, to be honest I don't recommend nesting more than one level deep.. urls get way out of hand really quickly..

something you may want to consider would be:

resources :groups do 
  resources :projects, except: [:index], controller: 'groups/projects
end

resources :projects, except: [:index], controller: 'groups/projects do
  resources :tasks, except: [:index], controller: projects/tasks
end

then your controllers would look like:

class Groups::ProjectsController < ApplicationController

end

class Projects::TasksController < ApplicationController

end

you would then have to create the following directories in the controllers folder:

groups(folder) => projects_controller.rb
projects(folder) => tasks_controller.rb

and then in the views directory you would do the same thing and nest the views to their respective parent.

in your forms, you would do something like

form_for(@group) do |f|
form_for(@group, @project) do |f|
form_for(@project, @task) do |f|

This is a fantastic link http://guides.rubyonrails.org/routing.html

but I would strongly recommend that you avoid deep nesting of routes.

hope this helps!

Shawn Wilson
  • 1,311
  • 14
  • 40
  • Thanks, Shawn. Given the Groups->Projects->Tasks parent-child set up, what would you do to avoid deeply nesting the routes if you were building this? I have seen articles on avoiding deep nesting and shallow routing, have tried a couple times to get it right but have run into some of the same form_for issues. Any help is appreciated! Thanks again! – CAKFinn Nov 04 '16 at 18:52