Getting my head around referrers

i’m trying to get my login and logouts to redirect to from where they
came. but because the login/logout controller runs twice (once when u
call it, and then when u login/logout) the referrer changes to the
login page. how can i prevent this?

here’s my code. tx:

def login
@from = params[:id]
session[:referer] = request.referer
@referer=session[:referer]
if request.post?
user = User.authenticate(params[:name], params[:password])
if user
session[:user_id] = user.id
session[:user_name] = user.name
redirect_to(session[:referer])
#redirect_to(:controller => “posts”, :action => “index”)
else
flash[:notice] = “Invalid user/password combination”
end
end
end

actually i solved this myself.
but it doesn’t seem to elegant.

any suggestions would be appreciated. here’s the code:

def login
if request.get?
@from = params[:id]
session[:referer] = request.referer
@referer=session[:referer]
end
if request.post?
user = User.authenticate(params[:name], params[:password])
if user
session[:user_id] = user.id
session[:user_name] = user.name
redirect_to(session[:referer])
else
flash[:notice] = “Invalid user/password combination”
end
end
end