Ruby-on-rails 链接回表单之前访问过的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16213176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:52:24  来源:igfitidea点击:

Link back to page visited before form

ruby-on-railsurlredirectrequestrefer

提问by user1738017

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.

但这只会将他们带回上一页,因此表单。

采纳答案by 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.

好吧,您可以在表单页面中设置一个方法来收集该 url。基本思想是使用自定义会话变量来存储先前的 url 并将其保留到下一个会话。

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

假设你的表单的 action 是 SomeController#new,那么

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_urlby

然后在感谢页面中,您可以my_previous_url通过

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

回答by panicoper

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.

这里有更多细节。

回答by ilgam

You can use the example from Rails API:

您可以使用 Rails API 中的示例:

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

Rails API Doc for link_to

link_to 的 Rails API 文档

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).

使用 :back 符号而不是选项哈希将生成指向引用者的链接(如果不存在引用者,将使用 JavaScript 反向链接代替引用者)。

回答by Nich

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.

既然你说,它可能是表单之前的不同页面,可能 request_url 可以帮助你。您可以将 request_url 保存在 param 中,如果有,则重定向到 param_url。

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

这是您可以参考的来源。 http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html

回答by zw963

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

如果在Controller中使用,你可以像这样直接使用:

def some_action
  # some code
  redirect_to :back
end

回答by Sheldon Hage

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

使用原始页面的 URL 在浏览器上设置 cookie

In the controller from the current view:

在当前视图的控制器中:

redirect_to cookies[:original_referrer]