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

**URL:** <https://www.ruby-forum.com/t/nested-model-form-javascript-question-re-ryan-batess-railscasts-196-7/184300>\
**Category:** Rails\
**Created:** [February 28, 2010, 3:53pm UTC](https://www.ruby-forum.com/t/nested-model-form-javascript-question-re-ryan-batess-railscasts-196-7/184300 "2010-02-28T15:53:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/91b2a8/32.png) [@Grary](https://www.ruby-forum.com/u/Grary)\
**Post date:** [February 28, 2010, 3:53pm UTC](https://www.ruby-forum.com/t/nested-model-form-javascript-question-re-ryan-batess-railscasts-196-7/184300/1 "2010-02-28T15:53:41Z")

</div>

Hi,

I might guess there are some loyal fans of Ryan B.'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”)  
$(link).up().insert({  
after: content.replace(regexp, new\_id)  
});  
}

from application\_helper.rb…

def link\_to\_add\_fields(name, f, association)  
new\_object =  
f.object.class.reflect\_on\_association(association).klass.new  
fields = f.fields\_for(association, new\_object, :child\_index =\>  
“new\_#{association}”) do |builder|  
render(association.to\_s.singularize + “\_fields”, :f =\> builder)  
end  
link\_to\_function(name, h(“add\_fields(this, “#{association}”,  
“#{escape\_javascript(fields)}”)”))  
end

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,

Grary
