2013-03-20 29 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 

創造了一個新的動作和我創建一個同名的視圖形式從中我可以查看記錄並保存它很容易

相關問題