Naroor Rathish
2005-Dec-23 05:52 UTC
adding items to a particular invoice on the same page
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
<html>
<head>
<title>Ajax List Demo</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h3>Add to list using Ajax</h3>
<%= form_remote_tag(:update => "my_list",
:url => { :action => :add_new_item },
:position => "bottom" ) %>
New item text:
<%= submit_tag "Add" %>
<%= end_form_tag %>
<form action="/ajax/save_items" method="post">
<ul id="my_list">
<input type="text" id="invoice_inv_number"
name="invoice[inv_number]" >
<li>Original item... please add more!</li>
</ul>
<%= submit_tag ''Update'' %>
</form>
</body>
</html>
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 Rathish
