Ruby on Rails 中的“返回”浏览器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/620962/
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:02:07  来源:igfitidea点击:

'Back' browser action in Ruby on Rails

ruby-on-railsbrowser

提问by alamodey

Can the 'Back' browser functionality be invoked from a Rails 'Back' link?

可以从 Rails 的“返回”链接调用“返回”浏览器功能吗?

采纳答案by Sophie Alpert

In Rails 3 and earlier:

在 Rails 3 及更早版本中:

link_to_function "Back", "history.back()"

In Rails 4, this method has been removed. See Andreas's comment.

在 Rails 4 中,此方法已被删除。见安德烈亚斯的评论。

回答by Tilendor

Use

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

This is specificied in the RDoc here

这在此处的 RDoc 中有详细说明

This generates some Javascript to navigate backward. I've just tested it, and it works.

这会生成一些 Javascript 来向后导航。我刚刚测试了它,它有效。

回答by HarlemSquirrel

This is working in Rails 5.1 along with Turbolinks.

这在 Rails 5.1 中与 Turbolinks 一起工作。

link_to 'Back', 'javascript:history.back()'

回答by Jay Killeen

In Rails 4.2, I got it to work with this:

在 Rails 4.2 中,我让它与这个一起工作:

<a href="javascript:history.back()">Refine Search</a>

I stole this off of @cpm's answer, except that link_to("Refine Search", :back)didn't do the job I wanted while pasting in the generated code <a href="javascript:history.back()">Refine Search</a>did it perfectly.

我从@cpm 的回答中窃取了这个,只是link_to("Refine Search", :back)在粘贴生成的代码时<a href="javascript:history.back()">Refine Search</a>没有完成我想要的工作。

回答by cpm

You can use link_to("Hello", :back)to generate <a href="javascript:history.back()">Hello</a>.

您可以使用link_to("Hello", :back)生成<a href="javascript:history.back()">Hello</a>.

回答by Pradeep Agrawal

This will work similarly as browser back button try this

这将与浏览器后退按钮类似,试试这个

<%= link_to 'Back', 'javascript:history.go(-1);' %>

<%= link_to 'Back', 'javascript:history.go(-1);' %>

回答by Philippe Addor

Pay attention to this commentfrom the user rthbound! As he notes, link_towith the symbol :backdoes not always generate a “real” back event as if the user clicked on their browser's Back button. It can also be a resubmit of the action that loaded the current view.

请注意来自用户rthbound 的这条评论!正如他所指出的,使用符号并不总是像用户单击浏览器的后退按钮一样生成“真正的”后退事件。它也可以是加载当前视图的操作的重新提交。link_to:back

The documentation for Rails 4.2.6says this about link_toand the :backsymbol:

Rails 4.2.6文档说明了这一点link_to:back符号:

Using a :backSymbol 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).

使用:backSymbol 而不是 options 哈希将生成指向引用的链接(如果引用不存在,将使用 JavaScript 反向链接代替引用)。

回答by Dan Andreasson

If you like me do not want the behaviour of link_to "cancel", :backyou could implement a helper method which either links to the records index path or show path. (i.e teams_pathor team_path(@team)

如果你喜欢我不想要link_to "cancel", :back你的行为,你可以实现一个帮助方法,它要么链接到记录索引路径,要么显示路径。(即teams_pathteam_path(@team)

module CancelFormButtonHelper
  def cancel_button(record)
    index_path = record.class.to_s.pluralize.downcase + "_path"
    path = record.persisted? ? record : eval(index_path)

    link_to "Cancel", path
  end
end

which can then be used as <%= cancel_button @team %>within a form for example.

例如,它可以<%= cancel_button @team %>在表单中使用。

回答by Evan Ross

You can use js function window.history.back()

你可以使用js函数window.history.back()

 = link_to 'Back', onclick: "window.history.back();"

回答by Mark A

Using

使用

link_to_function "Back", "history.back()"

seems to be exactly like hitting the back button in the browser. All inputted form data is still there when you get back.

似乎就像点击浏览器中的后退按钮一样。当您回来时,所有输入的表单数据仍然存在。