Method render_to_string() changes Content-Type in @headers. Lets see the following scenario:
# action from Ajax, returns javascript (parsed form.rjs)
def form
xml = get_xml()
# ...
end
# It returns parsed getx_xml.rxml
def get_xml
return render_to_string(:action=> 'get_xml', :layout => false)
end
get_xml() method changes @headers. So if I had text/javascript, now I have application/xml as Content-Type and I cannot use RJS files for action 'form'. This bug is difficult to trace. I had to use FireBug (Firefox plugin) to find what's was wrong. An here is my solution. Instead of
module ActionController
class Base
def render_to_string(options = nil, &block)
result = render(options, &block)
erase_render_results
forget_variables_added_to_assigns
reset_variables_added_to_assigns
result
end
end
end
the code should be:
module ActionController
class Base
def render_to_string(options = nil, &block)
content_type = @headers['Content-Type']
result = render(options, &block)
erase_render_results
forget_variables_added_to_assigns
reset_variables_added_to_assigns
@headers['Content-Type'] = content_type if content_type != @headers['Content-Type']
result
end
end
end