简体   繁体   English

使用devise登录后重定向到当前页面

[英]Redirecting to current page after sign in using devise

I am using devise for user sign in/up. 我正在使用devise进行用户登录/注册。 I want to redirect the user from on the page where they were before login. 我想从登录之前的页面上重定向用户。 But devise is not redirecting properly. 但是设计不能正确重定向。 On sign in, it signs in user but throws error on /user/sign_in page: 登录后,它会登录用户,但会在/ user / sign_in页面上引发错误:

"Firefox has detected that the server is redirecting the request for this address in a way that will never complete." “ Firefox已经检测到服务器正在以永远无法完成的方式重定向对该地址的请求。”

When i go back and reload the page that it shows user is logged in. 当我返回并重新加载该页面时,它显示用户已登录。

My application controller looks like: 我的应用程序控制器如下所示:

class ApplicationController < ActionController::Base
 protect_from_forgery
  def after_sign_in_path_for(resource)
   stored_location_for(resource) || request.referer || root_path
  end
  def after_sign_out_path_for(resource_or_scope)
   request.referrer
  end
end

I have used bootstrap modal script on product buy page. 我在产品购买页面上使用了引导模式脚本。 It works perfectly for login button used in modal script on page. 它非常适合页面上的模式脚本中使用的登录按钮。 But gives improper redirecting error for login button in header and hence for every page of app. 但是会给标题中的登录按钮以及应用的每个页面提供不正确的重定向错误。

What would be the reason? 请问是什么原因?

For your method that will store the user's location before being redirected to a sign in page you will want something like this. 对于在重定向到登录页面之前将存储用户位置的方法,您将需要类似以下内容。

  before_filter :store_location

  def store_location
    session[:user_return_to] = request.fullpath
  end

  def after_sign_in_path_for(resource)
   session[:user_return_to] || request.referrer || root_path
  end

This will store the location when a user enters your website in a session variable and then redirect the user to that session variable once they have logged in. 当用户在会话变量中输入您的网站时,它将存储位置,然后在用户登录后将用户重定向到该会话变量。

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

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