Domanda

Hi i know it's silly but true i got this when i edit any entry.

I have an expense model and an expense_line_item and a paid_line_item model, it is created properly when i create a new entry but on editing previous entry it adds a new entry instead of updating, i. e. at update action it fires insert query. here is my code:

My controller:

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

My Model:

  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

My form :

<%= 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 %>

i got frustrated, thanks in advance .

È stato utile?

Soluzione

i have found a solution for this problem. When i tried to update a lineitem it take it as a new one hence i have to pass a hidden lineitem_id for update action. i have used below code :

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

and it works for me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top