How can I reload the current page in Ruby on Rails?

后端 未结 9 1505

I currently have a login popup in my header bar which is on every page in my website. I want to be able to reload the current page that the person is on after a successful

相关标签:
9条回答
  • 2020-12-28 12:39

    Archonic's answer above worked for me. However, in Rails 3, I had to place this in a respond_to block in order to avoid an 'ArgumentError (too few arguments)' error:

    respond_to do |format|
      format.js {render inline: "location.reload();" }
    end
    
    0 讨论(0)
  • 2020-12-28 12:43

    Building on Archonic's and Elena's Answers, the reload function accepts a parameter to force the page to reload from the server aka forceGet, instead of from cache. A parameter can be set by the controller logic, like successful or failed login of a user, to trigger the desired behavior when it is sent to the page.

    # force_get = controller_logic ? true : false
    
    respond_to do |format|
      format.js { render inline: "location.reload(#{force_get});" }
    end
    

    UPDATE: the logic has been deprecated for the forceGet option. If you do want to reload from the server you can use this logic:

    # force_get = controller_logic ? true : false
    
    respond_to do |format|
      format.js { render inline: force_get ? "window.location.href = window.location.href;" : "location.reload();" }
    end
    
    0 讨论(0)
  • 2020-12-28 12:44

    just redirect to whatever url you want in the function:

    redirect_to what_ever_path

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