views:

85

answers:

1

I have some code I need to use in multiple controllers in a rails 1.0 application (I can't, for strange reasons upgrade to a newer rails). I've extracted the relevant code into a filer object, and I'm using the around_filter construct to execute it.

Before the extract, I was using the method render_to_string() to get the contents of a rendered partial into a string. However, this method is protected, so I am unable to access it from within my Filter object. As a workaround, I tried adding this to my ApplicationController:

def render_to_string(*a)
  super(*a)
end 

this seems to have remedied the protection level issue, but now I get the error:

Can only render or redirect once per action

When no such error occurred before the extraction. Why? Is there a different approach I should take here?

A: 

As far as I can tell , an action will call render prior to executing any filters. Since render_to_string works by running render, saving the result to a string and essentially undoing the render, the "Can only render or redirect once per action" comes up because the temporary call to render comes after the "real" call to render.

I fixed this, by simply calling my after method at the end of my action rather than using after_filter or around_filter.

I also ended up moving my filters into a Mix-in, to more elegantly avoid the protection level issue.

Tristan Havelick