更新操作触发插入查询?

时间:2012-02-10 11:06:24

标签: ruby-on-rails-3 sqlite3-ruby

嗨,我知道这很愚蠢,但是当我编辑任何条目时,我得到了这个。

我有一个费用模型和expense_line_item以及paid_line_item模型,它在我创建新条目时正确创建,但在编辑上一个条目时,它会添加一个新条目而不是更新,i。即在更新操作时,它会触发插入查询。这是我的代码:

我的控制器:

def new
        @menu = 'Expenses'
        @page_name = 'Record New Expenses'
    @expense = Expense.new
        @expense.expense_line_items.build
        @expense.paid_line_items.build
        @expense.voucher_number = "EXP"+Time.now.to_i.to_s
        @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
        @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments') 

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @expense }
    end
  end

  # GET /expenses/1/edit
  def edit
    @menu = 'Expenses'
    @page_name = 'Edit Expenses Entry'
    @expense = Expense.find(params[:id])
        @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
        @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments') 
  end

  # POST /expenses
  # POST /expenses.xml
  def create
    @expense = Expense.new(params[:expense])
      @expense.created_by = current_user.id
        @expense.company_id = current_company.id

    respond_to do |format|
      if @expense.save
        format.html { redirect_to(@expense, :notice => 'Expense was successfully created.') }
        format.xml  { render :xml => @expense, :status => :created, :location => @expense }
      else
        @menu = 'Expenses'
        @page_name = 'Record New Expenses'
                @from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
                @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')                
        format.html { render :action => "new" }
        format.xml  { render :xml => @expense.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /expenses/1
  # PUT /expenses/1.xml
  def update
    @expense = Expense.find(params[:id])

    respond_to do |format|
      if @expense.update_attributes(params[:expense])
        format.html { redirect_to(@expense, :notice => 'Expense was successfully updated.') }
        format.xml  { head :ok }
      else
        @menu = 'Expenses'
        @page_name = 'Edit Expenses Entry'
        format.html { render :action => "edit" }
        format.xml  { render :xml => @expense.errors, :status => :unprocessable_entity }
      end
    end
  end

我的模特:

  expense model:


 class Expense < ActiveRecord::Base
       has_many :expense_line_items
      has_many :paid_line_items

      accepts_nested_attributes_for :expense_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
      accepts_nested_attributes_for :paid_line_items, :reject_if => lambda {|a| a[:account_id].blank? }, :allow_destroy => true

      #validations
      validates_presence_of :expense_date, :voucher_number
      validates_presence_of :expense_line_items
      validates_associated :expense_line_items
      validates_presence_of :paid_line_items
      validates_associated :paid_line_items


    end

expense_line_item:

class ExpenseLineItem < ActiveRecord::Base
    belongs_to :expense
end

paid_line_item:

class PaidLineItem < ActiveRecord::Base
    belongs_to :expense
end

我的表格:

<%= form_for(@expense) do |f| %>

 <% @expense.expense_line_items.each_with_index do |expense_line_item, index| %> 
                      <%= render "expense_line_items", :expense_line_item => expense_line_item, :index => index %>  
                   <% end %>
                   <tr id="row_link" valign="top">
                      <td valign="top" colspan="6">
                           <%= link_to "Add new row",{:action => :add_row, :index => @expense.expense_line_items.size}, :remote => true %>
                      </td>
                   </tr>
 <% @expense.paid_line_items.each_with_index do |paid_line_item, index| %> 
                     <%= render "paid_line_items", :paid_line_item => paid_line_item, :index => index %>  
                   <% end %>
                  <tr id="to_row_link" valign="top">
                    <td valign="top" colspan="6">
                      <%= link_to "Add new row",{:action => :add_to_row, :index => @expense.paid_line_items.size}, :remote => true %>
                    </td>
                  </tr>
<% end %>

我很沮丧,先谢谢了。

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。当我尝试更新一个lineitem时,它将它作为一个新的,因此我必须传递一个隐藏的lineitem_id进行更新操作。我用过 下面的代码:

<%= hidden_field_tag "expense[expense_line_items_attributes][#{index}][id]",expense_line_item.id%>

它适用于我。