Rob Sanheim
2006-Nov-16 18:37 UTC
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 begin @cache = render_to_string(:file => self.find_some_file) rescue ActionController::MissingTemplate => e logger.debug("file not found, eating exception and continuing...") end @posts = Post.find... # other stuff here, normal processing, etc render end 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 -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2006-Nov-16 18:50 UTC
Re: should this be patched? render_to_string and MissingTemplate
On 11/16/06, Rob Sanheim <rsanheim@gmail.com> wrote:> > 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?Please do. Rendering to a string is pretty ugly: notice how render_to_string in an after_filter will wipe the main response.body. jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Sanheim
2006-Nov-20 10:09 UTC
Re: should this be patched? render_to_string and MissingTemplate
On 11/16/06, Jeremy Kemper <jeremy@bitsweat.net> wrote:> On 11/16/06, Rob Sanheim <rsanheim@gmail.com> wrote: > > 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? > > Please do. Rendering to a string is pretty ugly: notice how render_to_string > in an after_filter will wipe the main response.body. > > jeremy > > >I''ve patched the case where render_to_string will clobber instance variables. Patch with tests is here, should be pretty easy to apply. http://dev.rubyonrails.org/ticket/6658 thanks, Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---