Home‎ > ‎

Back tips

To redirect the user back to the previous page you can use the 
  redirect_to :back
command

Otherwise you can just use the HTTP_REFERER from the client request
  redirect_to request.env['HTTP_REFERER']

You can also store the previous page into a session variable and use it to create a link_to tag in the view page where you need it
  in the controller:
  session[:previous_page] = request.env['HTTP_REFERER']
  in the view
  <% session[:previous_page] %>
    <%= link_to "Back",  session[:previous_page] %>
  <% end %>

As we can't rely on HTTP_REFERER passed by the client we can modify the controller code so that we always have a good link. So you can add the url of a common page such as the root or the list of the products as your environment suggest
  session[:previous_page] = request.env['HTTP_REFERER'] || root_url

Comments