views:

13

answers:

2

I'm using an around filter which on certain occasions should redirect user to other path AFTER an action has been yielded (either ending with render or redirect).

Writing redirect_to in around filter after yield statement results in double render error. Trying to "reconfigure" response object (by setting body to nil and location header to desired url) results in folloing error (taken from controller specs):

Failure/Error: Unable to find matching line from backtrace
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Has anybody done anything similar to this?

A: 

You can't yield and then redirect/render afterwards. If you need to use some of the logic in the controller action in your around filter, consider moving that logic to a before_filter, or the first block of your around filter so that its available both in the around filter to determine what to do (redirect, or otherwise yield), and also available to the action you will be yielding to

Jeremy
That would be the case if that around filter applied only to single action. Now it's more of a "pluginized" around filter which could be applied to any action in any controller. Still, thanks for your insight.
Eimantas
yeah, if it's a generic around filter, that's when I'd suggest the specific logic be placed in a before_filter just on the actions it's needed to keep the around_filter clean (and still be able to use the data when available)
Jeremy
A: 

actually I manged to do redirect in around filter after action has been yielded. You just have to redefine the response.location attribute and reset the response.status to 302 (instead of 200).

Eimantas