开发者

How do I render a partial to a string from within a filter attached to a controller

开发者 https://www.devze.com 2022-12-26 21:32 出处:网络
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\

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 Appl开发者_开发百科icationController:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消