Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link back to page visited before form

I have a listing page, then a form, then a thank you page. I need to put a link on the thank you page that takes the user back to the page they were on before the form which always varies. I've tried using this:

= link_to "Back", :back 

But this only takes them back to the previous page, so the form.

like image 202
user1738017 Avatar asked Apr 25 '13 11:04

user1738017


2 Answers

Try this

<%= link_to 'Back', url_for(:back) %> # if request.env["HTTP_REFERER"] is set to "http://www.example.com" # => http://www.example.com 

here is more details.

like image 57
panicoper Avatar answered Sep 19 '22 15:09

panicoper


Well, you can set a method in the form page to collect that url. The basic idea is to use a custom session variable to store previous url and keep it to next session.

Suppose your form's action is SomeController#new, then

class SomeController < ApplicationController   after_action "save_my_previous_url", only: [:new]    def save_my_previous_url     # session[:previous_url] is a Rails built-in variable to save last url.     session[:my_previous_url] = URI(request.referer || '').path   end  end 

Then in the thank you page, you can get this my_previous_url by

 session[:my_previous_url] 

This should be able to suit your case, the previous url two pages ago.

Disclaimer: This is not verified. Idea only.

Add

Session belongs to controller. It is not a helper you can use directly in view. You need to define an instance variable in controller and then you can use it in view. Like this

# Controller @back_url = session[:my_previous_url]   # View <%= link_to "Back", @back_url %> 
like image 22
Billy Chan Avatar answered Sep 21 '22 15:09

Billy Chan