您好我正在创建一个操作来复制rails3中新记录的现有记录,该记录正确地填充了从先前记录到新记录的值,但在我提交表单时它会给出错误。这是我的代码示例
class SalaryStructuresController < ApplicationController
def new
@salary_structure = SalaryStructure.new
@salary_structure.salary_structure_line_items.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @salary_structure }
end
end
def create
@salary_structure = SalaryStructure.new(params[:salary_structure])
@salary_structure.company_id = current_company.id
@salary_structure.created_by = current_user.id
respond_to do |format|
if @salary_structure.valid?
@salary_structure.save_with_variable_payheads
flash[:success]= "Salary structure successfully created."
format.html { redirect_to(@salary_structure) }
format.xml { render :xml => @salary_structure, :status => :created, :location => @salary_structure }
else
format.html { render :action => "new" }
format.xml { render :xml => @salary_structure.errors, :status => :unprocessable_entity }
end
end
end
#action to clone the salary structure
def copy
@payheads = current_company.payheads
@users = current_company.users
@source_salary_structure = SalaryStructure.find(params[:id])
@salary_structure = SalaryStructure.new(@source_salary_structure.attributes)
@source_salary_structure.salary_structure_line_items.each do |line_item|
salary_item = SalaryStructureLineItem.new(line_item.attributes)
@salary_structure.salary_structure_line_items << salary_item
end
render :action => "new"
end
end
我的模特:
class SalaryStructure < ActiveRecord::Base
has_many :salary_structure_line_items
belongs_to :user
# has_many :payheads
accepts_nested_attributes_for :salary_structure_line_items, :reject_if => lambda {|p| p[:payhead_id].blank? && p[:amount].blank? }, :allow_destroy => true
#validation
validates_presence_of :effective_from_date, :for_employee
validates_presence_of :salary_structure_line_items
validates_associated :salary_structure_line_items
attr_accessible :id, :effective_from_date, :salary_structure_line_items_attributes, :amount, :total, :pay_head_type, :for_employee, :pay_head, :created_by, :updated_at, :created_at, :company_id,
:salary_structure_line_items_attributes
end
当我提交表格时(按保存),我收到了salary_structure_id的错误:
ActiveRecord::RecordNotFound in SalaryStructuresController#create
Couldn't find SalaryStructureLineItem with ID=11 for SalaryStructure with ID=
即使在参数salary_structure_id存在:
"commit"=>"Save",
"salary_structure"=>{"salary_structure_line_items_attributes"=>{"0"=>{"amount"=>"3000.0",
"_destroy"=>"",
"salary_structure_id"=>"4",
"id"=>"11",
"payhead_id"=>"1"},
"1"=>{"amount"=>"500.0",
"_destroy"=>"",
"salary_structure_id"=>"4",
"id"=>"12",
"payhead_id"=>"2"}
我无法追查我遗失的地方,请帮助我。
答案 0 :(得分:0)
我已经非常简单地创建了克隆文件,我在控制器中创建了一个新动作
def copy_salary_structure
@users = current_company.users.without_salary_structure
@payheads = current_company.payheads.where(:optional => false)
@old_salary_structure = SalaryStructure.find_by_id(params[:id])
@salary_structure = SalaryStructure.new
@salary_structure.company_id = @old_salary_structure.company_id
@salary_structure.created_by = current_user.id
@old_salary_structure.salary_structure_line_items.each do |line_item|
salary_structure_line_item = SalaryStructureLineItem.new(
:payhead_id => line_item.payhead_id,
:amount => line_item.amount
)
@salary_structure.salary_structure_line_items << salary_structure_line_item
end
end
我创建了一个具有相同名称的视图表单,我可以从中查看记录然后轻松保存