加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 登录或注册后,Devise会重定向到原始位置吗?

发布时间:2020-12-16 20:28:44 所属栏目:百科 来源:网络整理
导读:在这里,我使用Devise Gem进行身份验证.如果有人想要打开页面而不登录,那么它会重定向到sign_in页面,并在登录后返回到用户尝试打开的页面. 我使用 Redirect loop with Devise after_sign_in_path_for链接为我的问题,但它不适用于我. def after_sign_in_path_f
在这里,我使用Devise Gem进行身份验证.如果有人想要打开页面而不登录,那么它会重定向到sign_in页面,并在登录后返回到用户尝试打开的页面.
我使用 Redirect loop with Devise after_sign_in_path_for链接为我的问题,但它不适用于我.
def after_sign_in_path_for(resource)
   params[:next] || super 
 end

它不会将我重定向到我要打开的页面.
例如:如果我要打开“127.0.0.1:3000/post/2/edit”,登录后不会回到这个页面.

解决方法

最好的资源是官方的回购/维基/问题,然后是SO.你发现的答案已经过时了.

这是答案:
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-update

只需在ApplicationController中添加以下内容

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读