3/13更新:
我做了一個小樣本項目,我的模型,控制器邏輯和一些形式的版本。添加多個嵌套通過複選框Rails的4個屬性(也許多種形式)
我建立一個形式,用戶可以在其中添加「任務」和「里程碑」在一起。 (即Task ='Vacuum'在Milestone ='clean House'裏面)。它基本上是一個任務/子任務類型模型,父母是「里程碑」,孩子是「任務」。
這兩個任務和里程碑都屬於「項目」....所以我想通過嵌套形式添加任務和里程碑更新行動。我正在考慮的方式是爲每個@task_template實例創建一個表單並一次更新多個表單。
我的問題是,我也試圖通過表稱爲「MilestoneTemplates」和「TaskTemplates」動態設置「首發里程碑/任務」 ......
用戶拉起「添加里程碑/任務」頁面, ,這取決於他們的項目類型,他們在複選框旁邊看到一系列預構建任務(@task_templates)&里程碑(@milestone_templates)。用戶然後選中他們想要添加的任務或里程碑旁邊的複選框。這應該創建一個預置的@ task_template.name用戶特定的任務,@ task_template.description ...等
我不能讓這甚至創造1.我用Rails 4,我想我有正確設置我的strong_params。下面是我對這個地方:
型號:
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
控制器:
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
形式測試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 %>
表測試2(試圖提交陣列表格):
<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>
根據xcskier56在評論中的請求,我已經從Chrome檢查器中添加了我的POST代碼。正如你所看到的,表格甚至不會調用Tasks,只是父級里程碑。里程碑的形式顯示出來,但任務不....
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][]:
請求發送到服務器的結構是什麼樣的?在(在鉻)形式數據下查看檢查器中的網絡事件。 – xcskier56 2015-03-11 22:12:21
我已經更新了我的帖子和帖子請求。謝謝。 – NothingToSeeHere 2015-03-12 15:25:28