using ajax in nested attributes form

How can I use ajax to add nested attributes dynamically into the form
of an object?

I followed the railscast : http://railscasts.com/episodes/74-complex-forms-part-2

but I'd like to use Ajax instead because the method above is sort of
ugly.

Have you seen http://railscasts.com/episodes/196-nested-model-form-part-1
and part 2? It does not use ajax but I would not describe it as ugly.

Colin

yes I'm currently using those last railscasts methods but it's getting
pretty difficult to use the method in the edit form.

I am trying to follow an ajax method that should insert a new nested
attirubte form upon clicking a submit button in a form_remote_for. I
put this in the create.rjs

association = :treatments
new_object =
@sample.class.reflect_on_association(association).klass.new

fields = :new_sample.fields_for(association, new_object, :child_index
=> "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end

page.insert_html :bottom, :sample, fields

but I don't know how I can refer to the parent object's form in order
to call field_for. ":new_sample" doesn't work. Any ideas?

Mlle wrote:

How can I use ajax to add nested attributes dynamically into the form
of an object?

I followed the railscast :
http://railscasts.com/episodes/74-complex-forms-part-2

but I'd like to use Ajax instead because the method above is sort of
ugly.

No, actually, you don't want to use Ajax here. That will mean a round
trip to the server each time you want to add a field. Bad idea. This
should all be done on the client side.

Best,