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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 anonymous coward (Guest)
on 2006-06-01 08: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
requested...
my loggin system is this:


APPLICATION CONTROLLER:

def secure
redirect_to :controller => 'login'  unless @session[:admin] == 1
end
-------------------
in my LOGIN CONTROLLER:

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
FIX
        else
	   @session[:admin] = 0
      	   flash[:notice] = 'נת×?× ×?ם ש×?×?×?×?ם, אנא נס×? שנ×?ת.'
	   redirect_to :action => 'login'
        end
end
-------------------

(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;
thanks.
hola,

shai
octava
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-06-01 08:06
> 	   redirect_to :controller => 'messages'  ####THIS IS WHAT I NEED TO
> FIX


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..
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 anonymous coward (Guest)
on 2006-06-01 08:41
 -========detailed=========-

this is the way the admin is redirected:

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

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

def edit
secure
...
end

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)

shai
Da828b27a295fe67c31a4d64f751d6cd?d=identicon&s=25 Victor Fan (victorfan88)
on 2006-06-01 17: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
end

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

Victor
This topic is locked and can not be replied to.