3/13更新:
我已经用我的模型, Controller 逻辑和几种表格版本制作了一个小样本项目。
我正在建立一个表单,用户可以在其中添加“任务”和“里程碑”。 (即Task ='Vacuum'位于Milestone ='clean House'内)。它基本上是一个任务/子任务类型模型,父模型为“里程碑”,子模型为“任务”。
任务和里程碑都属于“项目”。...因此,我试图通过带有更新操作的嵌套表单来添加任务和里程碑。我在想办法是为每个@task_template实例创建一个表单,然后一次更新多个表单。
我的问题是,我还试图通过称为“MilestoneTemplates”和“TaskTemplates”的表动态设置“入门里程碑/任务”。
用户拉出“添加里程碑/任务”页面,并根据项目类型,在复选框旁边看到一系列预先构建的任务(@task_templates)和里程碑(@milestone_templates)。然后,用户选中要添加的任务或里程碑旁边的复选框。这应该为用户创建一个具有预建的@ task_template.name,@ task_template.description ... etc的特定任务。
我什至无法创建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
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
表格测试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代码。如您所见,该表单甚至不调用任务,而仅调用父里程碑。里程碑显示在表单中,但任务没有...。
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][]:
最佳答案
我还无法亲自测试该代码,但是我已经实现了类似的代码,因此这些想法应该是正确的。
这里的技巧是使用each_with_index,然后将该索引传递给fields_for
调用。这样,您通过复选框添加的每个其他milestone_id
将与之前的显着不同。您可以找到此here的另一个示例。
使用这种方法,您的表单应如下所示:
<%= form_for @project do |f| %>
<% @milestones_templates.each_with_index do |milestone, index| %>
<br>
<%= f.fields_for :milestones, index: index do |fm| %>
<%= fm.hidden_field :name, value: milestone.name %>
<!-- Create a checkbox to add the milestone_id to the project -->
<%= fm.label milestone.name %>
<%= fm.check_box :milestone_template_id,{}, milestone.id %>
<br>
<% milestone.task_templates.each_with_index do |task, another_index| %>
<%= fm.fields_for :tasks, index: another_index do |ft| %>
<!-- Create a checkbox for each task in the milestone -->
<%= ft.label task.name %>
<%= ft.check_box :task_ids, {}, task.id %>
<% end %>
<% end %>
<br>
<% end %>
<% end %>
<br>
<%= f.submit %>
<% end %>
# Working strong parameters.
params.require(:project).permit(:name, :milestones => [:name, :milestone_ids, :tasks => [:task_ids] ] )
这应该输出带有嵌套在其中的每个task_template_ids的Milestone_template_ids。
编辑:我忘了,如果您查看文档,则check_boxes需要在中间的另一个参数
f.checkbox :task_ids, task.id
应该实际上是以下内容:f.checkbox :task_ids, {}, task.id
现在寻找答案的肉。尽管此表单确实有效,并且有足够的麻烦,但我认为您可以通过轨道自动地更新项目并通过嵌套的属性来创建所需的一切,但我认为这不是一个好的设计。
更好的设计是使用builder类。它只是一个PORO(普通的旧Ruby对象)。这将允许您做的是围绕构建器编写良好的测试。因此,您可以放心,它将始终有效,并且对rails的一些更改不会破坏它。
以下是一些伪代码,可助您一臂之力:
ProjectsController << ApplicationController
def update
@project = Project.find(params[:id])
# This should return true if everything works, and
result = ProjectMilestoneBuilder.perform(@project, update_params)
if result == false
# Something went very wrong in the builder
end
if result.errors.any?
#handle success
else
# handle failure
# The project wasn't updated, but things didn't explode.
end
end
private
def update_params
params.require(:project).permit(:name, :milestones => [:name, :milestone_ids, :tasks => [:task_ids] ] )
end
end
在/lib/project_milestone_builder.rb中
class ProjectMilestoneBuilder
def self.perform(project, params)
milestone_params = params[:project][:milestones]
milestone_params.each do |m|
# Something like this
# Might be able to use nested attributes for this
# Milestone.create(m)
end
return project.update_attributes(params)
end
end
在/spec/lib/project_milestone_builder_spec.rb中
descibe ProjectMilestoneBuilder do
# Create a template and project
let(:template) {FactoryGirl.create :template}
let(:project) {FactoryGirl.create :project, template: template}
# Create the params to update the project with.
# This will have to have dynamic code segments to get the appropriate milestone_template_ids in there
let(:params) { "{project: {milestones ..." })
descibe '#perform' do
let(:result) { ProjectMilestoneBuilder.perform(project, params) }
it {expect(result.id).to eq project.id}
# ...
end
end
使用这种模式,您将得到一个封装良好,易于测试的类,该类将完全按照您的期望进行操作。快乐的编码。