0

Hi i am creating an action to copy an existing record in a new one in rails3 which is correctly populates the values from previous record to new one but at the time i submit the form it gives the error. Here is my code sample

 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 

My model:

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

When i submit the form (press save) i got the error on salary_structure_id:

 ActiveRecord::RecordNotFound in SalaryStructuresController#create

Couldn't find SalaryStructureLineItem with ID=11 for SalaryStructure with ID=

Even in parameters salary_structure_id is present :

"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"}

i am unable to trace where i am missing something, please help me.

4

1 回答 1

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

我创建了一个同名的视图表单,我可以从中查看记录,然后轻松保存

于 2013-06-21T12:28:40.507 回答