'Back' browser action in Ruby on Rails

前端 未结 10 912
无人共我
无人共我 2020-12-23 13:24

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

相关标签:
10条回答
  • 2020-12-23 13:39

    This is working in Rails 5.1 along with Turbolinks.

    link_to 'Back', 'javascript:history.back()'
    
    0 讨论(0)
  • 2020-12-23 13:41

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

     = link_to 'Back', onclick: "window.history.back();"
    
    0 讨论(0)
  • 2020-12-23 13:42

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

    0 讨论(0)
  • 2020-12-23 13:44

    If you like me do not want the behaviour of link_to "cancel", :back you could implement a helper method which either links to the records index path or show path. (i.e teams_path or team_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.

    0 讨论(0)
  • 2020-12-23 13:45

    In Rails 3 and earlier:

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

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

    0 讨论(0)
  • 2020-12-23 13:45

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

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

    0 讨论(0)
提交回复
热议问题