# Link\_to - Going Back without knowing the action name

**URL:** <https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961>\
**Category:** Rails\
**Created:** [May 9, 2006, 12:16am UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961 "2006-05-09T00:16:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 9, 2006, 12:16am UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961/1 "2006-05-09T00:16:51Z")

</div>

Hi,

I have a bunch of link\_to’s in my code that are simple Back buttons that  
should basically just go to the previous request. Is there a way of  
doing  
this without specifying the action to execute? The problem is,  
sometimes I  
may render the same view through separate actions so I wouldn’t know  
which  
action to go back to.

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 9, 2006, 1:23am UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961/2 "2006-05-09T01:23:57Z")

</div>

On May 8, 2006, at 3:15 PM, Sam D. wrote:

> I have a bunch of link\_to’s in my code that are simple Back buttons  
> that should basically just go to the previous request. Is there a  
> way of doing this without specifying the action to execute? The  
> problem is, sometimes I may render the same view through separate  
> actions so I wouldn’t know which action to go back to.

Consider passing a :return\_to parameter or session variable along  
with helper methods to manage your back links. For example:

class ApplicationController \< ActionController::Base  
helper\_method :url\_back, :link\_back

protected  
# Try params[:return\_to] and session[:return\_to].  
def url\_back(options = {})  
params[:return\_to] or session[:return\_to] or options  
end

```
 # Redirect back to where we came.
 def redirect_back(options = {})
   redirect_to(url_back)
 end

 # Link back to where we came.
 def link_back(name, fallback_options = {}, html_options = nil)
   link_to name, url_back(fallback_options), html_options
 end

```

end

Best,  
jeremy

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 27, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961/3 "2006-06-27T23:59:42Z")

</div>

> ```
> # Link back to where we came.
> def link_back(name, fallback_options = {}, html_options = nil)
> link_to name, url_back(fallback_options), html_options
> end
> 
> ```

I tried this and am getting an “undefined method `link\_to’ . . .” error  
when I try this. Any suggestions?

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 27, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961/4 "2006-06-27T23:59:54Z")

</div>

> ```
> # Link back to where we came.
> def link_back(name, fallback_options = {}, html_options = nil)
> link_to name, url_back(fallback_options), html_options
> end
> 
> ```

I tried this and am getting an “undefined method `link\_to’ . . .” error.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [June 28, 2006, 8:45am UTC](https://www.ruby-forum.com/t/link-to-going-back-without-knowing-the-action-name/59961/5 "2006-06-28T08:45:31Z")

</div>

```
  @session['return-to'] = @request.request_uri
  # as apposed to redirect to \ redirect to URL
  redirect_to_url @session['return-to']
  #to reset the session-return-to
  @session['return-to'] = nil

```

haven’t checked this, but i hope it works/helps,

shai
