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

后端 未结 9 1569

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

提交回复
热议问题