Getting a Rails action to return to where it was called from

July 04, 2008

Often in Rails you will want to use a partial in more than one place. Such partials might add or delete things. Once this is done you will want to return to where you were. The following works without javascript and Ajax …

The information we want is called the HTTP_REFERER. This is contained within the request object which is hugely complex. The request contains an env hash and the HTTP_REFERER is in there.


Now you can use this in your controller code e.g.

respond_to do |format|
  format.html { redirect_to request.env["HTTP_REFERER"]}