Can after_filter contain redirect_to?

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?

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?

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

redirect_to :back might give you what you’re looking for.

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!

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

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