57

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

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
EverTheLearner
  • 7,040
  • 16
  • 57
  • 72

9 Answers9

58

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.

datalost
  • 3,765
  • 1
  • 25
  • 32
  • 2
    This works for me to. You aren't reloading the current page, as much as you are going to a create action and then back again... If you use AJAX, then you'd just update the part where you show a dude's logged in... and keep him on that page. – pjammer Sep 19 '11 at 01:49
  • 2
    Documentation for redirect_to: http://api.rubyonrails.org/classes/ActionController/Redirecting.html – Nate Jan 24 '14 at 19:52
  • 37
    For Rails 5, `redirect_to :back` has been deprecated to be removed in 5.1, and instead `redirect_back(fallback_location: fallback_location)` should be used. – DuckNG May 24 '16 at 22:25
51

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
Archonic
  • 5,207
  • 5
  • 39
  • 55
28

In Rails 5 redirect_to :back is improved by:

    redirect_back(fallback_location: root_path)
bishal
  • 676
  • 7
  • 12
  • It works well with Rails 6 in 2020, btw `redirect_to :back` seem does not work for me – Chau Giang Jun 24 '20 at 06:54
  • Worth noting that flash messages work with this method too, e.g.: `redirect_back fallback_location: root_path, notice: "Job done"` – Simone Feb 09 '23 at 16:11
20

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"

Shobhit
  • 647
  • 1
  • 7
  • 19
17

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
Community
  • 1
  • 1
7

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

Kiryl Plyashkevich
  • 2,157
  • 19
  • 18
2

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
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Dec 11 '20 at 10:42
  • This solution works perfectly for my rails 6 application! Where did you happen to come across this method? It took quite a bit of looking for me. – anthony Feb 20 '21 at 19:26
0

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
SMAG
  • 652
  • 6
  • 12
0

just redirect to whatever url you want in the function:

redirect_to what_ever_path

Mahan Mashoof
  • 141
  • 1
  • 5