Forum: Ruby on Rails Going back to where you came from...

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.
F7975423060c2c9e4395dca3ae870771?d=identicon&s=25 De Railed (kitesurfer)
on 2005-12-01 07:37
Hi,

   I have a page (A) that contains a table with links in one column.
When the user hits a link it goes to another page (B) which is a read
only form page with a done button. This page (B) is being reused in
various parts of the app read pages (C) and (D). Thus when the user
clicks 'Done' it needs to return back to where she comes from ie either
(A), (C) or (D). I thought with the new Rails 0.14.3 I could just do
redirect_to :back and all my ills would be cured.
But.... this redirects me back to where I currently am ie (B).

   I am sure this is a very common problem out there. In my java life I
would create something in the session and consume it when this exact
situation arises.

   Is there a magic rails spell that I could use for this situation ? I
am not sure how to get the URL of page (A) so I can stach it in the
session for later retrieval or somehow push both the controller and
action of the page where the request was originated (??)

   Sample code please...

Thank you all !!
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-12-01 09:22
(Received via mailing list)
On 1.12.2005, at 8.37, KiteSurfer KiteSurfer wrote:

> But.... this redirects me back to where I currently am ie (B).
> action of the page where the request was originated (??)
>
>    Sample code please...

   def authenticate
   	unless session[:user_id]
   	  session[:return_to] = @request.request_uri
   	  redirect_to :controller => "login"
   	  return false
   	end
   end

# login_controller.rb

   def login
     if @user = User.authenticate(params[:name], params[:password])
       session[:user_id] = @user.id
       if session[:return_to]
         redirect_to(session[:return_to])
         session[:return_to] = nil
       end
     end
   end

(lots of lines omitted warning)

//jarkko
A43430351a6d4fb6d61d000bd56ed19a?d=identicon&s=25 francois (Guest)
on 2005-12-01 09:38
(Received via mailing list)
here is what we did - i think you can easily adapt it to fort your
particular issue


http://albert.bagasie.com/RailsTips/JumpBack
F7975423060c2c9e4395dca3ae870771?d=identicon&s=25 De Railed (kitesurfer)
on 2005-12-01 15:29
Good stuff. Thank you both !!
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-12-02 02:38
(Received via mailing list)
On Dec 1, 2005, at 12:18 AM, Jarkko Laine wrote:

>> either
>> situation ? I
>> am not sure how to get the URL of page (A) so I can stach it in the
>> session for later retrieval or somehow push both the controller and
>> action of the page where the request was originated (??)
>>
>>    Sample code please...

   def store_location
     @session['return-to'] = @request.request_uri
   end


   # move to the last store_location call or to the passed default one
   def redirect_back_or_default(default)
     if @session['return-to'].nil?
       redirect_to default
     else
       redirect_to_url @session['return-to']
       @session['return-to'] = nil
     end
   end

Then you can put a call to store location before you redirect to a
new page. Then once they finish with that page you call
redirect_back_or_default  to send them to the location stored in the
session.

Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.