In our app, a selection box is added dynamically to new rfq form. The problem is instead of the view, the code gets printed on the screen. The code is based on railscasts.com #197 with some modification.
Here is the code in rfqs/_form_new.html.erb:
<div id="std">
<%= render :partial => 'standards', :collection => @rfq.standards, :locals => { :f => f } %>
</div>
<p><%= link_to_add_fields "Add Std", f, :standards, @rfq %></p>
Here is the screen shot after clicking “Add Std”:

Here is the link_to_add_fields:
def link_to_add_fields(name, f, association, obj)
if association == :standards
new_object = obj.standards.build
else
new_object = obj.test_items.build
end
link_to_function(name, "add_fields(this, \"#{association}\", \"escape_javascript(render :partial => association.to_s, :collection => new_object, :locals => { :f => f })\")")
end
Here is the add_fields:
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
The relationship between rfq and standard is HABTM. In railscasts.com example, the relationship is has_many and belongs_to.
Thank you so much for the help.
The issue is that you are passing in a string with escape_javascript… in it to the link_to_function.
You probably want to render the partial immediately and put that in the link_to_function