# Redirect\_to :back question

**URL:** <https://www.ruby-forum.com/t/redirect-to-back-question/78910>\
**Category:** Rails\
**Created:** [March 9, 2006, 10:23am UTC](https://www.ruby-forum.com/t/redirect-to-back-question/78910 "2006-03-09T10:23:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 9, 2006, 10:23am UTC](https://www.ruby-forum.com/t/redirect-to-back-question/78910/1 "2006-03-09T10:23:36Z")

</div>

Hi!

How to use this? I can declare it only inside a controller, so i tried  
to do add this to application controller:

def back  
redirect\_to :back  
end

so i could call in all my views  
link\_to ‘back’, :controller =\> ‘application’, :action =\> ‘back’

but it doesn’t work - there are no errors, but i’m still on the same  
page.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 9, 2006, 10:35am UTC](https://www.ruby-forum.com/t/redirect-to-back-question/78910/2 "2006-03-09T10:35:23Z")

</div>

It does work – except that when you click on that link, you go from  
your current page to the ‘back’ action, which will redirect you back to  
the current page. Why? Because your current page will effectively be the  
referrer URL! What you need is a helper called link\_to\_back or so, which  
will return the value of link\_to request referrer. I actually have found  
it nice to have link\_to\_back to take in a :default option because you  
may not always have a referring page.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 9, 2006, 10:32am UTC](https://www.ruby-forum.com/t/redirect-to-back-question/78910/3 "2006-03-09T10:32:21Z")

</div>

it has to be used in the controller…

On Thursday, March 09, 2006, at 10:23 AM, szymek wrote:

> link\_to ‘back’, :controller =\> ‘application’, :action =\> ‘back’
> 
> but it doesn’t work - there are no errors, but i’m still on the same  
> page.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 9, 2006, 11:31am UTC](https://www.ruby-forum.com/t/redirect-to-back-question/78910/4 "2006-03-09T11:31:12Z")

</div>

Thanks!!!

I supspected that this may be the case (because there were no errors),  
but didn’t know how to do it in another way.

Now I’ve got something like this:

def link\_to\_back(text)  
link\_to text, request.env[“HTTP\_REFERER”]  
end

How can i add this default parameter (as a hash) that you’ve mentioned?

And one more thing - is there an easy way to check if user has  
javascript enabled? I could do:  
link\_to\_function text, “history.go(-1)”  
and if user doesn’t have javascript call  
link\_to text, request.env[“HTTP\_REFERER”]
