# Can after\_filter contain redirect\_to?

**URL:** <https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483>\
**Category:** Rails\
**Created:** [March 29, 2007, 4:23am UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483 "2007-03-29T04:23:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 4:23am UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/1 "2007-03-29T04:23:05Z")

</div>

I want to occasionally render a certain page after an action (other  
than the default rendering). I was thinking that an after\_filter  
would be the ticket. However, my initial attempts at including  
redirect\_to in an after\_filter result in a double render. Is the  
after\_filter the right tool, or is there a better way?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/2 "2007-03-29T15:24:13Z")

</div>

Let me state this another way:

Can I use redirect\_to in an after filter to render some view other  
than the default view? If so, how?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/3 "2007-03-29T20:37:13Z")

</div>

You can only call redirect or render once… so I don’t think you can do  
this.

Looks like ActionController calls performed? to see if redirect or  
render has already been called (probably a private method). Don’t see  
any way to reset the performed? status. But, you could probably hack  
ActionController to allow you to do so.

b

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/4 "2007-03-29T21:18:34Z")

</div>

redirect\_to :back might give you what you’re looking for.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/5 "2007-03-29T21:09:31Z")

</div>

Well, there must be some way to get the same effect. Let’s say I’m  
looking at a blog, I am not logged in, and I want to write a comment.  
I can’t yet, because I’m not logged in, so I’m redirected to the login  
screen. After I submit my user/pass I want to go back to the comment  
page, not to wherever the login action normally goes to.

I know someone must have done this!

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/6 "2007-03-29T22:06:43Z")

</div>

Rick wrote:

> Well, there must be some way to get the same effect. Let’s say I’m  
> looking at a blog, I am not logged in, and I want to write a comment.  
> I can’t yet, because I’m not logged in, so I’m redirected to the  
> login  
> screen. After I submit my user/pass I want to go back to the comment  
> page, not to wherever the login action normally goes to.
> 
> I know someone must have done this!

I’ve done what you’re talking about here by storing the request\_uri in  
the session, sending the user to log in and then having code in the  
login controller that redirects the user to the uri in the session if  
it’s there, or to the default url if not.

That’s all controller logic (the authentication stuff I usually do as a  
before\_filter in ApplicationController)… I think your dilemma – if  
I’m understanding correctly – is that you want to change the render  
decision that’s already been made in an action in an after\_filter.  
_That_ is tricky.

If you’re just trying to do an authentication filter, I’d try making it  
a before\_filter. That way, if they’re not authenticated, the redirect  
takes them to log in and there’s no double-render. If they are  
authenticated, there’s no redirect and they wind up in the action they  
intended to call… and still there’s no double-render.

If you have the agile book, they show this approach in a fair amount of  
detail… and it’s fairly simple to add the code to that to just store  
off the requested uri and get it back out to send the user to after  
they’ve logged in.

HTH,

b

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/can-after-filter-contain-redirect-to/90483/7 "2007-03-29T21:54:41Z")

</div>

Are you talking about something like…

def catcher\_filter  
unless logged\_in?  
session[:original] = request.path  
redirect\_to login\_url  
end  
end

def login\_method

# do the login

if login\_successful?  
if session[:original]  
redirect\_to session[:original]  
session[:original] = nil # Don’t forget to reset!  
else  
redirect\_to dashboard\_url  
end  
end  
end

RSL
