简体   繁体   English

使用Devise登录后重定向到上一页

[英]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. 我正在尝试使用Devise来实现此功能,但是除了重定向到首页之外,我无法做其他任何事情。

I need to put something in my after_sign_in_path_for method. 我需要在after_sign_in_path_for方法中放入一些after_sign_in_path_for

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: 显然,Devise默认情况下会存储先前的位置,但是我尝试自己进行以下操作:

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 您可以尝试与此一起使用request.referer

redirect_to request.referer

this question could also provide more insight 这个问题也可以提供更多的见解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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