Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6073 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

[PATCH] render_to_string changes Content_Type

Reported by: Jarosław Zabiełło <hipertracker@gmail.com> Assigned to: sandofsky
Priority: high Milestone:
Component: ActionPack Version: edge
Severity: normal Keywords: headers render_to_string
Cc:

Description

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  

Attachments

preserve_content_type_on_render_to_string.diff (2.3 kB) - added by sandofsky on 01/24/07 20:27:30.
Preserves content-type headers when you use render_to_string
preserve_content_type_on_render_to_string_tests.diff (1.2 kB) - added by josh on 06/25/07 23:13:40.
Units proving that there is no bug

Change History

(in reply to: ↑ description ) 11/26/06 01:58:24 changed by hipertracker

  • priority changed from normal to high.

Still nothing fixed....

(in reply to: ↑ description ) 01/24/07 20:26:30 changed by sandofsky

  • owner changed from David to sandofsky.
  • version changed from 1.1.6 to edge.
  • summary changed from render_to_string changes Content_Type to [PATCH] render_to_string changes Content_Type.

This affects edge. I've written a patch based on your code, with passing tests.

01/24/07 20:27:30 changed by sandofsky

  • attachment preserve_content_type_on_render_to_string.diff added.

Preserves content-type headers when you use render_to_string

06/25/07 23:13:40 changed by josh

  • attachment preserve_content_type_on_render_to_string_tests.diff added.

Units proving that there is no bug

06/25/07 23:14:08 changed by josh

  • status changed from new to closed.
  • resolution set to fixed.

Must be fixed in edge. Your unit tests and my new ones both pass on edge.