Justin,
With Rails 0.9.5 some changes were made to allow this kind of multiple
record updating on one page (the Wiki does not reflect these new
changes). Here''s an example of what you might do:
class PurchaseOrdersController < ApplicationController
def new
@purchase_order =
PurchaseOrder.new(@params[''purchase_order''])
@order_items = @purchase_order.items
end
end
Then, in your view (new.rhtml, for example):
<% @order_items.each do |@order_item| %>
<%= text_field "order_item[]", "name" %>
<%= text_field "order_item[]", "price" %>
... etc ...
<% end %>
The trailing square brackets after the object name (order_item[])
tells Rails to automatically add the index ("id" field) of that
order_item to the form element (a text field in this case). So you''ll
get HTML like this, for example:
<input type="text" name="order_item[33][name]" ... >
<input type="text" name="order_item[33][price]" ... >
<input type="text" name="order_item[39][name]" ... >
<input type="text" name="order_item[39][price]" ... >
This is especially handy for the new 0.9.5 "update" method which will
take a hash of hashes and update the correct IDs:
def create
OrderItem.update @params[''order_item''] #Updates both items
33 and 39
end
Hope this helps,
Duane Johnson
On Fri, 04 Feb 2005 15:43:42 -0800, Francisco Hernandez
<lagcisco-b7MHZcQsHeJWk0Htik3J/w@public.gmane.org>
wrote:> I believe you can do this using indexed params.. I''ve not done
this
> myself with rails but checkout this wiki page
>
> http://wiki.rubyonrails.com/rails/show/TutorialRelationalForms
>
> Justin Mitchell wrote:
> > Hey,
> >
> > I''m really new to RoR, so bare with me :). Anyways, I have a
situation
> > where I need to create multiple records across different tables in one
> > form.
> >
> > Let me explain. This page is to record purchase orders, and on this
> > single page we will give the details on the purchase order (date,
> > purchase_order_number, supplier, payment_method). Also, we need to
> > record each of the items that are on the purchase order (item,
> > quantity, description, cost, etc). There can be an unlimited number of
> > items on each purchase order.
> >
> > So far, my controller looks like this:
> >
> > *purchase_orders_controller*
> > def create
> > @purchase_order =
PurchaseOrder.new(@params[''purchase_order''])
> > @order_item =
OrderItem.new(@params[''order_item''])
> > if @purchase_order.save & @order_item.save
> > flash[''notice''] = ''PurchaseOrder was
successfully created.''
> > redirect_to :action => ''list''
> > else
> > render_action ''new''
> > end
> > end
> > *end*
> >
> > I believe this would be sufficient for a single Item, but I''m
not sure
> > how to do multiple items.
> >
> > Any suggestions?
> >
> > Thanks!
> > Justin
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>