Hi,
Problem occurs when I "Add more Parent". When the next parent is
added, I see one parent with mangled child form. And I think that is
because the child form goes through the helper method twice - one
method is the "add_parent_link" shown above and the other is
"add_child_link" (not shown, but does exactly the same as
"add_parent_link"). I hope I'm making sense here.
I run into the same problem.
I tried to pass an option "already_escaping" set to true if I
add_parent_link is called from a partial which was already called
add_parent_link.
And then, I used the options like this :
if options[:already_escaping]!=true
html = escape_javascript(html)
end
I even rewrote the escape_javascript to become "unescape_javascript"
like this :
JS_UNESCAPE_MAP = { '\\\\' => '\\', '<\/' => '</', '\n' => "\r\n",
'\n' => "\n" , '\n' => "\r" , '\\"' => '"' , "\\'" => "'" }
def unescape_javascript(javascript)
if javascript
javascript.gsub(/(\\\\|<\\\/|\\\n|\\"|\\'|\n)/) { JS_UNESCAPE_MAP
[$1] }
else
''
end
end
But still without success.
I think what's important is to understand what's beeing called and
when.
The children partial seems to be rendered while contructing the parent
one.
When it's rendered, the Javascript is escaped. And then when the
parent is rendered, the Javascript is escaped once again.
Tell me if I'm wrong.
It's not a solution to the problem, but maybe seeing it from our two
points of vue will help us find a solution 
The methods in my helper are like this :
def helper_remove_link(fields)
out = ''
out << fields.hidden_field(:_delete)
out << link_to_function("Supprimer", "$(this).up('.#
{fields.object.class.name.underscore}').hide(); $(this).previous
().value = '1'")
out
end
def helper_add_record_link(form_builder, method, caption, options =
{})
options[:object] ||=
form_builder.object.class.reflect_on_association(method).klass.new
options[:partial] ||= method.to_s.singularize
options[:form_builder_local] ||= :f
options[:insert] ||= method
link_to_function(caption) do |page|
form_builder.fields_for(method, options[:object], :child_index
=> 'NEW_RECORD') do |f|
html = render(:partial => options[:partial], :locals =>
{ options[:form_builder_local] => f })
if options[:already_escaping]!=true
html = escape_javascript(html)
end
page << %{
$('#{options[:insert]}').insert({
bottom: '#{html}'.replace(/NEW_RECORD/g, new Date
().getTime())
});
}
end
end
end
And I call the helper like this, to add a children :
<%= helper_add_record_link f, :contacts, 'Ajouter un contact...',
:insert => "#{prefix}contacts_#{f.object.id ? f.object.id :
'NEW_RECORD' }",
:already_escaping => true %>
Best regards,
Christian