Adding items to a particular invoice on the same page


#1

Hi,
I have been building a invoice generator(in ruby on rails) for
the
past some days and been stuck on the final page .This page will have
fields
for entering the invoice details and also I have added a AJAX effect so
that
fields for invoice items get created dynamically . its all design well
but
the issue comes when I am trying to save the page .That is adding ivoice
details to invoices table and invoiceitems details to invoiceitems table
.We
have to add the invoiceid to the invoiceitems table .I am adding all the
required code with this mail.

View Index.rhtml

Ajax List Demo <%= javascript_include_tag "prototype" %>

Add to list using Ajax

<%= form_remote_tag(:update => "my_list",
                   :url => { :action => :add_new_item  },
                   :position => "bottom" ) %>

  New item text:

  <%= submit_tag "Add" %>
   <%= end_form_tag %>
  • Original item... please add more!
<%= submit_tag 'Update' %>
</form>

Controller Ajax_controller.rb

lass AjaxController < ApplicationController

def index
$counter =1
$count = 0
end

def add_new_item

render(:partial => ‘invoiceitems’, :collection => @invoiceitems )

end

def save_items
# render_text params[:invoiceitem]
# render_text params[:invoice]
@invoice = Invoice.new(params[:invoice])

   # Error comes from these Lines  (NoMethodError in Ajax#save_items
 # undefined method `invoice_items' for #<Invoice:0x385b130> )

 params[:invoiceitem].each do |key, val|
	 @invoice.invoice_items << InvoiceItem.new(val)
 end

 if @invoice.save
	render_text "hello"
 end

end
end

The model classes

Invoice.rb
class Invoice < ActiveRecord::Base
has_many :invoiceitems
belongs_to :company

end

Invoice Items
class Invoiceitems < ActiveRecord::Base
belongs_to :invoice
end

Hope some body can help me out .

Thanking you,
Naroor R.