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

Ticket #6658 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Ensure render_to_string doesn't break instance variables

Reported by: rsanheim Assigned to: David
Priority: normal Milestone: 1.2
Component: ActionPack Version: edge
Severity: normal Keywords: tiny tests has_tests
Cc: bitsweat

Description

Currently, this will break instance variable assignment -- not the @after var - it will not make it into the view, as assigns are set in the first render_to_string and never reset due to the exception.

def render_to_string_with_caught_exception
    @before = "i'm before the render"
    begin
      render_to_string :file => "not found!!", :use_full_path => true
    rescue
    end
    @after = "i'm after the render" # this will never make it into @assigns
    render :action => "test/hello_world"
  end 

The use case that turned this up was where I was using render_to_string to load in some static html that corresponded to most of my users, but in a few cases we wouldn't have a matching file for new users. So I specifically rescued the MissingTemplate exception, but my processing I had after the render_to_string was useless as the results never made it to the view.

I've attached a patch with tests that just wraps the render_to_string call in a rescue block, and ensures that things are cleaned up after the render call even if we get an exception.

I also threw in a test case showing that render_to_string followed by a normal render does NOT throw a DoubleRender error, just to make that one specification clear in the code.

Attachments

render_to_string_fix.diff (4.1 kB) - added by rsanheim on 11/20/06 10:06:30.
patch to make sure render_to_string cleans up after itself

Change History

11/20/06 10:06:30 changed by rsanheim

  • attachment render_to_string_fix.diff added.

patch to make sure render_to_string cleans up after itself

11/20/06 10:49:23 changed by bitsweat

  • cc set to bitsweat.
  • component changed from ActiveRecord to ActionPack.

Additionally, render_to_string in an after_filter wipes the original response body..

11/20/06 11:03:21 changed by bitsweat

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

(In [5591]) Ensure render_to_string cleans up after itself when an exception is raised. Closes #6658. Great tests!