Rails 3.1 escape_javascript doesn't work as expected
I have the following Code:
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(th开发者_开发知识库is, '#{association}', '#{escape_javascript(fields)}')"))
end
When it runs it's supposed to duplicate the previous form field. When i click on my link to add a new field it just renders the HTML inline (it's been completely escaped and viewable). I found a previous question which said this was an issue with Rails 3.1.0rc2 but i'm running the current 3.1.0 release of rails. So i'm not sure if this something with my config or if the issue has reappeared in rails.
Try changing this line
link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
to this
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')", :remote => true)
I think* rails 3.1.x does the escaping automatically
精彩评论