3/13 UPDATE:
I've made a small sample project with my models, controller logic and several form versions.
I am building a form where a user can add "Tasks" and "Milestones" together. (ie. Task = 'Vacuum' is inside Milestone = 'clean House'). It's basically a Task/Subtask type model with the parent being 'Milestone' and the child being 'Task'.
Both Tasks and Milestones belong to "Project"....so I am trying to add the Tasks and Milestones through a nested form with an update action. I am thinking the way to go is create a form for each @task_template instance and update multiple forms at once.
My problem is that I am also trying to dynamically set "starter milestones/tasks" through tables called "MilestoneTemplates" and "TaskTemplates"....
The user pulls up the "Add Milestones/Task" page and, depending on their type of project, they see an array of prebuilt tasks(@task_templates) & milestones(@milestone_templates) next to checkboxes. The user then CHECKS the checkbox next to the task or milestone they would like to add. This should create a specific task for the User with a prebuilt @task_template.name, @task_template.description...etc
I cannot get this to even create 1. I am using Rails 4 and I think I have set my strong_params correctly. Below is where I am on this:
Models:
class Task < ActiveRecord::Base
belongs_to :user
belongs_to :project
belongs_to :milestone
class Milestone < ActiveRecord::Base
belongs_to :project
belongs_to :user
has_many :tasks, dependent: :destroy, inverse_of: :milestone
accepts_nested_attributes_for :tasks, allow_destroy: true
class Project < ActiveRecord::Base
has_many :milestones, dependent: :destroy
has_many :tasks, dependent: :destroy
accepts_nested_attributes_for :tasks, allow_destroy: true
accepts_nested_attributes_for :milestones, allow_destroy: true
#the "Starter Milestones & Tasks"
class MilestoneTemplate < ActiveRecord::Base
has_many :task_templates, dependent: :destroy, inverse_of: :milestone_template
class TaskTemplate < ActiveRecord::Base
belongs_to :milestone_template, inverse_of: :task_templates
Controller:
class ProjectsController < ApplicationController
def new_milestones
@project = Project.find(params[:p])
@project.milestones.build
@project.tasks.build
@milestones_templates = MilestoneTemplate.where(template_id: @project.template_id)
end
def create_milestones
@project.milestone_ids = params[:project][:milestones]
@project.task_ids = params[:project][:tasks]
@milestone = Milestone.new
@task = Task.new
@template = Template.find( @project.template_id)
if @project.update_attributes(project_params)
redirect_to view_milestones_path(p: @project.id)
flash[:notice] = "Successfully Added Tasks & Milestones"
else
redirect_to new_milestones_path(p: @project.id )
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
def project_params
params.require(:project).permit( :id, :name,
milestones_attributes: [:id, {:milestone_ids => []}, {:ids => []}, {:names => []}, :project_id, :user_id,
:name, :description, :due_date, :rank, :completed, :_destroy,
tasks_attributes: [:id, {:task_ids => []}, {:names => []}, {:ids => []}, :milestone_id, :project_id,
:user_id, :name, :description, :due_date, :rank, :completed, :_destroy]] )
end
end
Form Test 1:
<%= form_for @project, url: create_milestones_path(p: @project.id) do |f| %>
<label>Milestones</label><br>
<div class="row">
<%= hidden_field_tag "project[names][]", nil %>
<% @milestones_templates.each do |m| %>
<%= check_box_tag "project[names][]", m.name, @milestones_templates.include?(m), id: dom_id(m)%>
<%= label_tag dom_id(m), m.name %>
<%= hidden_field_tag "project[milestone][names][]", nil %>
<% m.task_templates.each do |t| %>
<%= check_box_tag "project[milestone][names][]", t.name, m.task_templates.include?(t), id: dom_id(t) %>
<%= label_tag dom_id(t), t.name %>
<% end %>
<% end %>
</div>
<%= f.submit %>
Form Test 2(trying to submit an array of forms):
<label>Milestones</label><br>
<%= hidden_field_tag "project[milestone_ids][]", nil %>
<% @milestones_templates.each do |m| %>
<div>
<%= f.fields_for :milestones do |fm|%>
<%= check_box_tag "project[milestone_ids][]", @milestones_templates.include?(m), id: dom_id(m) %>
<%= label_tag dom_id(m), m.name %></div>
<%= hidden_field_tag :name, m.name %>
<%= hidden_field_tag "project[milestone][task_ids][]", nil %>
<% m.task_templates.each do |t| %>
<%= fm.fields_for :tasks do |ft| %>
<%= check_box_tag "project[milestone][task_ids][]", t.name, m.task_templates.include?(t), id: dom_id(t)%>
<%= label_tag dom_id(t), t.name %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
as per xcskier56's request in the comments, I've added my POST code from Chrome inspector. As you can see, the form isn't even calling the Tasks, just the parent Milestones. The Milestones show up in the form, but the tasks don't....
project[formprogress]:2
project[milestone_ids][]:
project[milestone][names]:true
name:Milestone 1
project[milestone][task_ids][]:
project[milestone][names]:true
name:Milestone 2
project[milestone][task_ids][]:
project[milestone][names]:true
name:Milestone 3
project[milestone][task_ids][]:
project[milestone][names]:true
name:Milestone 4
project[milestone][task_ids][]: