Maybe not the most descriptive subject! :-) I have a filter that redirects the user to a login page if they have not logged in on certain pages. Now what is the best way to get the page they where initially requesting and then send the user to that page after they have successfully logged in? John Kopanas -- http://www.soen.info - where software engineering knowledge gets indexed http://cusec.soen.info - software engineering conference
You can do: redirect_to :back From the manual: ":back: Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to (request.env["HTTP_REFERER"])" Rob
PS: However, that only works if you have the login form embedded in your pages. If you have an link that takes the user to a intermediary login form, then the solution I proposed doesn''t work. There''s probably another way to do it then, but I don''t know about it. Rob
<ryan-efiCBTuagYofEnXWrtFoVw@public.gmane.org>
2005-Nov-13 23:17 UTC
Re: Best Way To Get Where You Have Been
Save it in the session before you redirect... session[:where_you_wanted_to_go] = request.parameters # Then redirect to login redirect_to (:controller => "login", :action => "login") The code below goes in your login controller and executes once login is successful: where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action => "default_page"} session[:where_you_wanted_to_go] = nil redirect_to(where_you_wanted_to_go) I may not have this 100%, I''m pulling it from memory, I believe I saw it covered in the Agile Rails Development book... Hope this helps, --Ryan ----- Original Message ----- From: "John Kopanas" <john-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Sunday, November 13, 2005 5:14 PM Subject: [Rails] Best Way To Get Where You Have Been> Maybe not the most descriptive subject! :-) > > I have a filter that redirects the user to a login page if they have not > logged in on certain pages. Now what is the best way to get the page > they where initially requesting and then send the user to that page after > they have successfully logged in? > > John Kopanas > > -- > http://www.soen.info - where software engineering knowledge gets indexed > http://cusec.soen.info - software engineering conference > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Most off the login systems spawned from the original login generator include two methods which try to handle this: store_location and redirect_back_or_default - you can look at the login generator, salted hash login generate, login engine and acts_as_authenticated generator/plugin for examples of these methods to copy, and how to use them in your own application. - james On 11/13/05, ryan-efiCBTuagYofEnXWrtFoVw@public.gmane.org <ryan-efiCBTuagYofEnXWrtFoVw@public.gmane.org> wrote:> Save it in the session before you redirect... > > session[:where_you_wanted_to_go] = request.parameters > # Then redirect to login > redirect_to (:controller => "login", :action => "login") > > The code below goes in your login controller and executes once login is > successful: > > where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action => > "default_page"} > session[:where_you_wanted_to_go] = nil > redirect_to(where_you_wanted_to_go) > > I may not have this 100%, I''m pulling it from memory, I believe I saw it > covered in the Agile Rails Development book... > > Hope this helps, > > --Ryan > > ----- Original Message ----- > From: "John Kopanas" <john-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Sent: Sunday, November 13, 2005 5:14 PM > Subject: [Rails] Best Way To Get Where You Have Been > > > > Maybe not the most descriptive subject! :-) > > > > I have a filter that redirects the user to a login page if they have not > > logged in on certain pages. Now what is the best way to get the page > > they where initially requesting and then send the user to that page after > > they have successfully logged in? > > > > John Kopanas > > > > -- > > http://www.soen.info - where software engineering knowledge gets indexed > > http://cusec.soen.info - software engineering conference > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Here is a link to a blog that helped me solve a similar problem: http://www.bigbold.com/snippets/tag/rails/2 I took the code from the site and added two minor changes to handle an empty session and new/create duplication and it works beautifully for me. Just add the redirect_back to your actions. Regards, Michael Application Controller: # Store where we are def store_locations if @session[''prevpage''] && @session[''thispage''] != @request.request_uri #If this is a "create" action, the do not do anything, because it will just take us to the new unless @request.request_uri.index(''create'') @session[''prevpage''] = @session[''thispage''] || '''' @session[''thispage''] = @request.request_uri end else @session[''prevpage''] = @request.request_uri @session[''thispage''] = @request.request_uri end end ..... # redirect_back # If a previous page is stored in the session, go back to it.. otherwise go back to a default page def redirect_back(default) if @session[''prevpage''].nil? if default redirect_to default else redirect_to :controller => "", :action => "" end else if @session[''prevpage''].length > 4 redirect_to_url @session[''prevpage''] else redirect_to default end end end ryan-efiCBTuagYofEnXWrtFoVw@public.gmane.org wrote: Save it in the session before you redirect... session[:where_you_wanted_to_go] = request.parameters # Then redirect to login redirect_to (:controller => "login", :action => "login") The code below goes in your login controller and executes once login is successful: where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action => "default_page"} session[:where_you_wanted_to_go] = nil redirect_to(where_you_wanted_to_go) I may not have this 100%, I''m pulling it from memory, I believe I saw it covered in the Agile Rails Development book... Hope this helps, --Ryan ----- Original Message ----- From: "John Kopanas" To: Sent: Sunday, November 13, 2005 5:14 PM Subject: [Rails] Best Way To Get Where You Have Been> Maybe not the most descriptive subject! :-) > > I have a filter that redirects the user to a login page if they have not > logged in on certain pages. Now what is the best way to get the page > they where initially requesting and then send the user to that page after > they have successfully logged in? > > John Kopanas > > -- > http://www.soen.info - where software engineering knowledge gets indexed > http://cusec.soen.info - software engineering conference > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails --------------------------------- Yahoo! FareChase - Search multiple travel sites in one click. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 13, 2005, at 2:52 PM, Robert wrote:> You can do: > > redirect_to :back > > From the manual: > > ":back: Back to the page that issued the request. Useful for forms > that are triggered from multiple places. Short-hand for redirect_to > (request.env["HTTP_REFERER"])"This won''t work: - redirect to /login - post form to /login - redirect :back sends you to the REFERER, which is /login You can use something like: def redirect_back(fallback_options) redirect_to params[:return_to] || session[:return_to] || fallback_options end and pass :return_to as a hidden form parameter or set session [:return_to] = request.request_uri before you redirect to login. jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDd93tAQHALep9HFYRAj0oAJ9RRn5NWnCzX78HyfizjsMpbFDEXwCgkzZ9 luDOitdtG2oMjonc1XwH0Jo=RAoG -----END PGP SIGNATURE-----
Forgot to add this for the application controller: # Store the current URL so we can redirect back to it if necessary before_filter :store_locations Michael <codeslush-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: Here is a link to a blog that helped me solve a similar problem: http://www.bigbold.com/snippets/tag/rails/2 I took the code from the site and added two minor changes to handle an empty session and new/create duplication and it works beautifully for me. Just add the redirect_back to your actions. Regards, Michael Application Controller: # Store where we are def store_locations if @session[''prevpage''] && @session[''thispage''] != @request.request_uri #If this is a "create" action, the do not do anything, because it will just take us to the new unless @request.request_uri.index(''create'') @session[''prevpage''] = @session[''thispage''] || '''' @session[''thispage''] = @request.request_uri end else @session[''prevpage''] = @request.request_uri @session[''thispage''] = @request.request_uri end end ..... # redirect_back # If a previous page is stored in the session, go back to it.. otherwise go back to a default page def redirect_back(default) if @session[''prevpage''].nil? if default redirect_to default else redirect_to :controller => "", :action => "" end else if @session[''prevpage''].length > 4 redirect_to_url @session[''prevpage''] else redirect_to default end end end ryan-efiCBTuagYofEnXWrtFoVw@public.gmane.org wrote: Save it in the session before you redirect... session[:where_you_wanted_to_go] = request.parameters # Then redirect to login redirect_to (:controller => "login", :action => "login") The code below goes in your login controller and executes once login is successful: where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action => "default_page"} session[:where_you_wanted_to_go] = nil redirect_to(where_you_wanted_to_go) I may not have this 100%, I''m pulling it from memory, I believe I saw it covered in the Agile Rails Development book... Hope this helps, --Ryan ----- Original Message ----- From: "John Kopanas" To: Sent: Sunday, November 13, 2005 5:14 PM Subject: [Rails] Best Way To Get Where You Have Been> Maybe not the most descriptive subject! :-) > > I have a filter that redirects the user to a login page if they have not > logged in on certain pages. Now what is the best way to get the page > they where initially requesting and then send the user to that page after > they have successfully logged in? > > John Kopanas > > -- > http://www.soen.info - where software engineering knowledge gets indexed > http://cusec.soen.info - software engineering conference > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails --------------------------------- Yahoo! FareChase - Search multiple travel sites in one click. --------------------------------- Yahoo! FareChase - Search multiple travel sites in one click. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails