2
votes

I'm using devise 4.0.0 and rails 4.2.6. I am trying to get devise to redirect to the page the user was at before they went to the devise log in page or before they clicked log out (which is a link to destroy_user_session_path). I have read the devise How To https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update which says that it's out of date and I should read the code https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/store_location.rb which I did, but that didn't help me figure out what I needed to do. I have searched through a bunch of SO Q/As, many of them also outdated. One of them worked for me for sign_in. I added:

before_filter :store_current_location, :unless => :devise_controller?

private  
  def store_current_location
    store_location_for(:user, request.url)
  end

to my ApplicationController. But sign out is still returning to root. I overrode after_sign_out_path_for(resource) and found out that stored_location_for(resource) is nil. I can't figure out why. Other SO Q/As suggested:

private
  def after_sign_out_path_for(resource)
    stored_location_for(resource) || request.referrer || root_path
  end

which seems to do the trick through the request.referrer part. I can't figure out where the stored location is being deleted. In the devise code I linked above, the stored location is only deleted when we retrieve it which doesn't seem to be happening.

I'm new to Rails, so there could be some really obvious thing I'm missing. Can anyone explain to me why stored_location_for(resource) is nil? Or where it's being deleted?

1
I assume (since I don't know for sure) that you're storing the location in a session variable that obviously gets destroyed on logout. - xyious
Yes! devise stores the location in a session variable. So obvious now that you've said it. Thank you. - Ingrid Biery
FWIW, I updated the devise documentation on github so hopefully this will be easier for others. - Ingrid Biery

1 Answers

0
votes

Try

request.referrer

in order to get the previous url