Link back to page visited before form

ⅰ亾dé卋堺 提交于 2019-12-02 18:59:28
Billy Chan

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_filter "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 %>

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.

You can use the example from Rails API:

<%= link_to "Back", :back %>

Rails API Doc for link_to

Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists).

Since you saying,it might be different page before form, probably request_url can help you. you can save your request_url in a param and redirect to param_url if there is.

here is a source that you can take for reference. http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html

If use in Controller, you can direct use like this:

def some_action
  # some code
  redirect_to :back
end

This works for me:

In controller from previous view:

cookies[:original_referrer] = request.orignal_url

to set a cookie on the browser with the URL of the originating page

In the controller from the current view:

redirect_to cookies[:original_referrer]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!