Eimantas
Eimantas

Reputation: 49354

Redirect in around filter after action has been yielded

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?

Upvotes: 0

Views: 1918

Answers (2)

Eimantas
Eimantas

Reputation: 49354

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).

Upvotes: 1

Jeremy
Jeremy

Reputation: 4930

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

Upvotes: 0

Related Questions