HOW TO create a new record and its associated objects within

hi,

can’t find any good tutorial or advice to deal with the creation of a
new record and it’s associated objects within the same page.

do i have to use ajax just to add custom form fields that will be
handled by the controller for associated objects creation.?

do i have to use ajax to add associated objects to the unsaved but
allready in session parent object?

what’s the best approach ?

thanks

On May 20, 2006, at 10:00 AM, olivier Hericord wrote:

what’s the best approach ?

I don’t think AJAX is necessary for any of this. I learned how to do
it from the AWDR book, specifically pages 102 through 107. If you
look at the #save_order action, it looks like:

def save_order
@cart = find_cart
@order = Order.new(params[:order]) # <-- this is from the
submitted form
@order.line_items << @cart.items # <— this automagically sets up
the keys
if @order.save
@cart.empty!
# whatever
else
#whatever
end
end

So I guess the advice is to do this work in the action/method that
handles the creation of the new record from a form. If you have the
proper #has_many and #belongs_to directives setup in the model, AR
will handle setting up all the foreign keys for you.

Or maybe I’m wrong (still a newbie).

cr

on a has_many relation ajax is essential because you don’t know how
many associated objects you wan to create within the parent creation.

so the form has to evolve while you are adding objects…no?

On Saturday, May 20, 2006, at 5:41 PM, olivier Hericord wrote:

if @order.save
will handle setting up all the foreign keys for you.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

So far as I can tell, the only way to avoid the use of AJAX in a form
for an object that may associate with many other items is to force the
user to enter the associated objects before calling the form. In some
cases this may not be a problem, but in many it is. For example…

If you have a Person model that has_many PhoneNumbers, it is fairly
impractical to expect the user to enter the phone numbers before
entering the person’s information.

It is possible to set up the form to accept a fixed number of sub
objects that will be associated. Following from the previous example,
you would need to set up form fields for 10 new phone numbers, and then
hope that was enough.


One approach that may work would be to store the main object and it’s
associated objects in the session and then save the whole lot when the
form is submitted.

_Kevin

i decided to put the unsaved parent object in sessiion … that
allows me to create via ajax associated object that are not lost if
the parent saving raise an error…

let’s take the example of the address book.

the main form is the person form.
so we have form fields like this one:

it would be cool to have an helper that can generate a field like this
one:

automagically the application will understand that it’s the array
containing the street field of all the addresses attached to the
person object

seems crazy?

One approach that may work would be to store the main object and it’s
associated objects in the session and then save the whole lot when the
form is submitted.

I’ve used this method quit a few times. Even if you decide to use AJAX
to associate the children with the parent, you’ll need to put the parent
object into a session first.

-Steven

with ajax it would be possible to generate for the first attached
address:


then for the second attached address:


and so on…

You definitely do not need to use asychronous server calls for this at
all.
Just use the DOM methods to create the form fields on the fly. Here is
a
page that has a quick rundown of the methods available to you:

Zack H.

In almost any case, an experienced web developer can accomplish what
you’re attempting without using AJAX. Whether you ask for the number
of associated items at once or walk the user through a wizard-like
flow (add another item or finish).

Validations can be acheived without using AJAX. Just retrieve them all
at once and display them.

If you plan to use AJAX in any application, you should be aware of
the implications that its use can have for your users.

http://www.standards-schmandards.com/index.php?2005/03/01/16-ajax-and-accessibility

and also

yes but ajax allows me to achieve validations.

On 5/22/06, Brian H. [email protected] wrote:

In almost any case, an experienced web developer can accomplish what
you’re attempting without using AJAX. Whether you ask for the number
of associated items at once or walk the user through a wizard-like
flow (add another item or finish).

Yupyup… I’d go with a wizard interface. It’s quick, easy, and a UI
pattern that users are familiar with. And it doesn’t rely on AJAX for
validations, you can just use standard validations at the proper
places.