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.
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.
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
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.
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:
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.