Forum: Ruby on Rails how can i redirect a person after a login

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
anonymous coward (Guest)
on 2006-06-01 10:00
i built my own login system, but unfortunately, i am having problems
(ie, can't really figure out how) to redirect a person back to the page
he was requesting before logging in...say, as the admin i want to edit a
certain message/logo, whatever, so i click on it, and i get redirected
to my login page. after the admin logs in, he gets redirected to the
homepage - i couldn't figure out how to redirect to the page he
my loggin system is this:


def secure
redirect_to :controller => 'login'  unless @session[:admin] == 1

def authenticate
 pswd  = [ 'shai', 'octava']
 @input = [  "#{params[:user][:name]}", "#{params[:user][:password]}"  ]
       if pswd == @input
           @session[:admin] = 1
      	   flash[:notice] = 'successful entry.'
	   redirect_to :controller => 'messages'  ####THIS IS WHAT I NEED TO
	   @session[:admin] = 0
      	   flash[:notice] = 'נת×?× ×?ם ש×?×?×?×?ם, אנא נס×? שנ×?ת.'
	   redirect_to :action => 'login'

(i put the secure method in any action i wish to protect, and walla!,
everythings good)(except the redirecting issue)
i know the answer should be something like @session[:url_path] = xyz,
and then redirect in the authenticate action to @session[:url_path], but
the problem is, i'm not really sure what the xyz part is (that stores
the current url the person is on) and i aint really sure what the
correct syntax for doing this is...

if anyone has an answer, clue, or just plain sympathy, i'll be ready;

Carmen -. (Guest)
on 2006-06-01 10:06
> 	   redirect_to :controller => 'messages'  ####THIS IS WHAT I NEED TO

redirect_back_or_default is probably what you want. check
acts_as_authenticated for a use case. it only seems to actualy redirect
back when your access to a page was 'interrupted' for needing login, say
youre on a page that normal users can only view, and special users can
additioanlly delete or add items, if you click on a link to
/account/login youll never come back to that page after logging in,
unless im missing something..
anonymous coward (Guest)
on 2006-06-01 10:41

this is the way the admin is redirected:

def secure
redirect_to :controller => 'login'  unless @session[:admin] == 1

and then i implement the secure method in any one of the actions i want
to protect, such as 'edit':

def edit

and then, when a person who is on the page
http://localhost:3000/messages wants to edit a message, say
http://localhost:3000/edit/3, he will automaticly be redirected to
http://localhost:3000/login/login, where in this action there will be a
user, and pswd field, and if he enters them correctly (which you can see
in the authenticate action, is 'shai' and 'octava') he will be
redirected to :controller => 'messages' (above where i wrote '#WHERE I
NEED TO FIX'). this is the place i need to somehow, (if possible??),
insert some kind of a parameter/variable that will give me the
path/url/action-conroller/whatever of the last page visited before the
'redirection' to the login page = = = i.e, after entering the user+pswd,
get redirected back to (examply obviously) http://localhost:3000/edit/3.

is there any way to do this??
(ps thanks for the reply)

Victor F. (Guest)
on 2006-06-01 19:03
I think a better to do is with before_filter call back function.

for example

def secure
   redirect_to :controller => 'login'  unless @session[:admin] == 1

Then in your edit controller

class EditController < ApplicationController
  before_filter :secure, :except => :login  #except is a method that
doesn't require login (you can put more in the except)

Hope this helps  :)

This topic is locked and can not be replied to.