# Q about returning to the action that called the method

**URL:** <https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540>\
**Category:** Rails\
**Created:** [August 8, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540 "2007-08-08T12:46:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 8, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540/1 "2007-08-08T12:46:33Z")

</div>

I have a few actions which, after completing, i want to return to the  
action that called them. Currently the way i’m doing this is passsing  
through a parameter with the name of the calling method, eg

\<%= link\_to ‘Delete this picture’, :action =\> ‘delete\_picture’, :id =\>  
picture, :came\_from =\> “edit\_story” %\>

Then in the callee method i refer to this in the redirect\_to part:

redirect\_to :action =\> params[:came\_from], :id =\> @story if  
params[:came\_from]  
flash[:warning] = “Couldn’t find a story to return to, returning to  
list”  
redirect\_to :action =\> “list”

Is there a nicer way to get the calling method, without having to  
remember to explicitly pass it through? Or a nicer alternative to what  
i’m doing, generally?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 8, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540/2 "2007-08-08T14:11:48Z")

</div>

Max W. wrote:

> Is there a nicer way to get the calling method, without having to  
> remember to explicitly pass it through? Or a nicer alternative to what  
> i’m doing, generally?
> 
> thanks  
> max

try to mess around with redirect\_to :back and see if it helps you…

M^

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 8, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540/3 "2007-08-08T14:34:02Z")

</div>

(continuing Mikkel’s post)

# redirect\_to :back

(taken from api)

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”])

The redirection happens as a “302 Moved” header.

When using redirect\_to :back, if there is no referrer, RedirectBackError  
will be raised. You may specify some fallback behavior for this case by  
rescueing RedirectBackError.

( [Peak Obsession](http://api.rubyonrails.com/classes/ActionController/Base.html#M000271)  
)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 8, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/q-about-returning-to-the-action-that-called-the-method/111540/4 "2007-08-08T15:31:11Z")

</div>

Mikkel B. wrote:

> try to mess around with redirect\_to :back and see if it helps you…

Trying this made me realise that when i delete a picture, i actually  
need to go back TWO steps, not just one. So i think i’m better off  
sticking with my explicit parameter passing method to avoid confusion.

“redirect\_to :back” is good to know though, might be useful in future.  
Thanks!
