2013-03-20 51 views
0

嗨,我正在创建一个行动来复制一个新的轨道中的现有记录,这是正确填充从前一个记录到新的值,但在我提交表单时,它给出的错误。这里是我的代码示例在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

我创建副本文件在这里非常简单的方法我已经在我的控制器

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 

创造了一个新的动作和我创建一个同名的视图形式从中我可以查看记录并保存它很容易