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

Ruby on-RailsRubyRuby on-Rails-3Url Routing

Ruby on-Rails Problem Overview


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 login. How do I do this in the controller?

def create
  #declaring and defining user variable stuff
  if user.save
    #reload current page <--how do I do this?
  end
end

Thanks

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

For my application, I use redirect_to :back and it does the trick. However, I doubt this might have an error in a non general use case(s) (user came from a special page?) but i haven't found it so far in my app.

Solution 2 - Ruby on-Rails

If you're looking for a way to get the page to refresh (typically redirect_to :back) with an XHR request, you don't have to look for a way to change the response type - just tell the page to reload with inline JS.

format.js { render inline: "location.reload();" }

Like Elena mentions, this should go in a respond_to block, like so:

respond_to do |format|
  format.js {render inline: "location.reload();" }
end

Solution 3 - Ruby on-Rails

In Rails 5 redirect_to :back is improved by:

    redirect_back(fallback_location: root_path)

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

Since Rails 5 (or maybe older versions), you have a request.referrer method. You simply redirect from controller to referrer and it opens the page where request came from.

redirect_to request.referrer, notice: "You're being redirected"

Solution 6 - Ruby on-Rails

Rails 5 introduced alternative function:

redirect_back(fallback_location: root_path)

It redirect back whenever the HTTP_REFERER is known. Otherwise it redirects to the fallback_location.

The redirect_to :back is deprecated in Rails 5.0 https://github.com/rails/rails/pull/22506 and removed since Rails 5.1

Solution 7 - Ruby on-Rails

This syntax is what you want... works in Rails 6

  respond_to do |format|
    format.html { redirect_to request.referrer, notice: "User was successfully WHATEVER." }
  end

Solution 8 - Ruby on-Rails

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

Solution 9 - Ruby on-Rails

just redirect to whatever url you want in the function:

redirect_to what_ever_path

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEverTheLearnerView Question on Stackoverflow
Solution 1 - Ruby on-RailsdatalostView Answer on Stackoverflow
Solution 2 - Ruby on-RailsArchonicView Answer on Stackoverflow
Solution 3 - Ruby on-RailsbishalView Answer on Stackoverflow
Solution 4 - Ruby on-RailsElena G. WashingtonView Answer on Stackoverflow
Solution 5 - Ruby on-RailsShobhitView Answer on Stackoverflow
Solution 6 - Ruby on-RailsKiryl PlyashkevichView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAnthony MasedaView Answer on Stackoverflow
Solution 8 - Ruby on-RailsSMAGView Answer on Stackoverflow
Solution 9 - Ruby on-RailsMahan MashoofView Answer on Stackoverflow