Forum: Ruby on Rails Storing URL locations

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.
Elias O. (Guest)
on 2008-11-26 18:32
(Received via mailing list)
I have a friendship system where users can create new friendships from
different views. The idea is to redirect them to where they initially
requested, accepted, etc... the friendship. To do this, I need to
store the URL path from where they are sending the request. Is there a
way to save the initial URL (from where the request mas made to the
friendships controller). Any light on this?

Thanks for your time,

Sean McGilvray (Guest)
on 2008-11-26 18:36
(Received via mailing list)
I use the following code to redirect them to the page they requested
they logged in.  Maybe this will get you on the right path.  I put it in
controller under protected.

# Protect a page from unauthorized access.
  def protect
    unless logged_in?
      session[:protected_page] = request.request_uri
      flash[:notice] = "Please log in first"
      redirect_to :action => "login"
      return false

  # Redirect to the previously requested URL (if present).
  def redirect_to_forwarding_url
    if (redirect_url = session[:protected_page])
      session[:protected_page] = nil
      redirect_to redirect_url
      redirect_to :action => "index"

Sean McGilvray & Sarena Byers
Executive Director
Identity Theft Specialist
Pre-Paid Legal Service's, Inc. NYSE:PPD
Phone:  760-486-1019
Elias O. (Guest)
on 2008-11-26 18:54
(Received via mailing list)
Hi Sean thanks for the response.

I thought about a solution based on your answer, but I am not happy
with it. My solution consisted on having a before_filter on the
controllers I know there might be a call to the friendships
controller. This before_filter will be used to save the URL. And then,
have a before_filter on the application controller where I do this:

if params[:controller] != "friendships"
  session[:return_to] = nil

The thing is that having this before_filter every time for every
controller doesn't seems right.


This topic is locked and can not be replied to.