Hi Wilson ,
I was trying your code to be implemented in mine but some errors are
coming .
I added a partial ::
<% @invoiceitem = invoiceitem -%>
<%= text_field ‘invoiceitem’, ‘item_price’, ‘index’ =>
invoiceitem_counter %>
<%= text_field ‘invoiceitem’, ‘item_qty’, ‘index’ =>
invoiceitem_counter
%>
Where are we initialising the counter? Please tell how is the first line
working .
In the rhtml file I have added :
<%= form_remote_tag(:update => "my_list",
:url => { :action => :add_new_item },
:position => "bottom" ) %>
<%= submit_tag "Add New Item" %>
<%= end_form_tag %>
- Original item... please add more!
<%= submit_tag 'Update' %>
I have added like this for creating a dynamic generation of text boxes
using ajax.
And in the controller class ,
def add_new_item
render(:partial => ‘invoiceitems’, :collection => @invoice_items)
end
def save_items
@items = []
params[:invoiceitem].each do |key, item|
@items = InvoiceItem.new(item)
end
end
Please help my in implementing this requirement .
Thanks and regards,
Naroor R…
Wilson B. wrote:
On 12/15/05, naroor rathish [email protected] wrote:
invoice details and the invoice items on the same button click.
One way would be to move the text fields you want generated to a
partial.
Let’s pretend it’s called _invoiceitem.rhtml
Then, in your RHTML file, where you currently have those text_field
statements…
Assuming @invoice_items is an Enumerable containing the AR objects
you want to work with.
<%= render(:partial => ‘invoiceitem’, :collection => @invoice_items) %>
_invoiceitem.rhtml would look something like: (First line is
important, if you want to use the form helpers.)
<% @invoiceitem = invoiceitem -%>
<%= text_field ‘invoiceitem’, ‘item_price’, ‘index’ =>
invoiceitem_counter %>
<%= text_field ‘invoiceitem’, ‘item_qty’, ‘index’ => invoiceitem_counter
%>
And so on, for the various attributes you want to display.
nameofpartial_counter is a local variable created for you that keeps
track of the index.
In the controller action that your form submits to, you’d do something
like:
@items = []
params[:invoiceitem].each do |key, item|
@items << InvoiceItem.new(item)
end
Now you’ve got an array of all the invoice item objects, and you can
do whatever you want with it… maybe stuffing it in the session for
later use, or calling “.valid?” on each entry and putting something in
the flash if any of them aren’t correct, etc.
Hopefully someone will come along and show a more elegant solution
that I just don’t know about yet, but this is roughly how I do it.
–Wilson.