简体   繁体   中英

Redirect to previous page after login with Devise

I'm trying to implement this functionality using Devise, but I can't get it to do anything other than redirect to the homepage.

I need to put something in my after_sign_in_path_for method.

I've tried:

def after_sign_in_path_for(resource)
  request.env['omniauth.origin'] || stored_location_for(resource) || root_url
  super
end

I've looked at this question and pretty much every other post regarding this on SO without any luck. Apparently Devise stores the previous locations by default, but I've tried doing it myself:

after_filter :store_location

def store_location
  # store last url as long as it isn't a /users path
  session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
end

def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end

I've done dozens of different combinations of the above and answers in other questions. Why don't any of these work? I don't even get errors or anything, it basically just ignores my code and redirects to the homepage no matter what I do.

you could try using request.referer with this

redirect_to request.referer

this question could also provide more insight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM