nested model form javascript question (re Ryan Bates's Railscasts #196-7)


I might guess there are some loyal fans of Ryan Bates's Railscasts out
there. For those who might be familiar with his two casts on nested
model forms (#196-7), I had a question...

How do I modify one (or both) of the javascript and helper functions,
respectively, that Ryan showed so that I can use them to append
multiple children? In other words, with the functions below I can
present only one model instance at a time, whereas I'd like to present
several different model instances with each call.

from application.js...

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
    after: content.replace(regexp, new_id)

from application_helper.rb...

  def link_to_add_fields(name, f, association)
    new_object =
    fields = f.fields_for(association, new_object, :child_index =>
"new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    link_to_function(name, h("add_fields(this, \"#{association}\",

What I think I need to do is figure out how to append to some
arbitrary place on the DOM, like the passed-in link element. I just
don't know how.

Thanks for any help,