should this be patched? render_to_string and MissingTemplate

Ran across this today using render_to_string along with a normal render:

def some_action
     @cache = render_to_string(:file => self.find_some_file)
  rescue ActionController::MissingTemplate => e
     logger.debug("file not found, eating exception and continuing...")
  @posts = Post.find...
  # other stuff here, normal processing, etc

The problem is that if render_to_string throws an exception, it never
does forget_variables_added_to_assigns and
reset_variables_added_to_assigns (see render_to_string source in
ActionController::Base). So @variables_added will equal true, and
future instance variables won't be added to assigns, and so of course
won't be available in the view. Thats bad.

I'm guessing render_to_string is often used in cases like this, where
you just want to grab some static file and its possible the file is
missing. So it seems it should handle the exception gracefully and
reset the variables added to assigns, so later processing in the
action works.

Does this make sense? Should I submit a patch to handle this correctly?

btw this is against 1.1.6. but from looking at edge it looks like the
same thing would happen.
- Rob

Please do. Rendering to a string is pretty ugly: notice how render_to_string in an after_filter will wipe the main response.body.


I've patched the case where render_to_string will clobber instance
variables. Patch with tests is here, should be pretty easy to apply.