Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:58:46+00:00 2026-05-29T16:58:46+00:00

Hi i know it’s silly but true i got this when i edit any

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:58:50+00:00Added an answer on May 29, 2026 at 4:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know we already have many posts about this topic, but I just cannot
know nothing about php, but I have this script that reads a folder and
I know I have done this before but I can't seem to remember where
Know of any good libraries for this? I did some searches and didn't come
Know this might be rather basic, but I been trying to figure out how
I know this might be a no-brainer, but please read on. I also know
I know, I would have thought the answer was obviously no as well, but
I know this is a stupid question, but I've looked for 45 mins now
I know very little about Agile but I wonder if there is any difference
I know the input have the maxlength, but I would like to have minlength

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.