Forum: Ruby on Rails redirect_to :back question

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.
Szymon N. (Guest)
on 2006-03-09 11:23

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

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
Mikkel B. (Guest)
on 2006-03-09 11:32
(Received via mailing list)
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
>Posted via
>Rails mailing list

Mikkel B.    - Football Portal(DK) - Football News(DK)      - Buy Old Stuff!(DK)
Kamal F. (Guest)
on 2006-03-09 11:35
(Received via mailing list)
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.
Szymon N. (Guest)
on 2006-03-09 12:31

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"]

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"]
This topic is locked and can not be replied to.