3+ layers of nested models on one form

i've read Ryan Bates' advanced rails recipe "Handle Multiple Models in
One Form" and i've implemented it for a model nested inside another
model and it works brilliantly. However i now need to nest another
model within the nested model.. ie: 'functions' are nested into a
'module' and each 'function' has nested 'parameters'

where it breaks down is in the 2nd layer of nesting where i am using
insert_html to insert the partial for an empty 'function' into the
'module' form. because the 'function' partial also contains
insert_html code for inserting 'parameters' the javascript generator
blows up..

my first inclination is to try to rewrite the insert_html in with
'plainer' js just as Mr. Ryan Bates did with his 'remove'
link_to_function. however i like the idea of being able to use the
handy javascript generators since it makes the code look so much nicer
and aam wondering if there's anyway to sanitize the javascript or
something..

if anyone wants to read the article to know what i'm talking about the
recipe is available on the pragmatic programmer's site as a free
downloadable sample from the book 'advanced rails recipes'..

thanks a million!

Stu