登录或注册后设计重定向回原始位置?

在这里,我使用Devise Gem进行身份validation。 如果有人想要在没有登录的情况下打开页面,那么它会重定向到sign_in页面,然后在登录后返回到用户尝试打开的页面。 我使用Redise循环和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,-sign-out,-sign-up, -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 

您不需要所有这些代码。 设计 (作为最新版本)已经为您保存了位置。

只需使用:

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

这会将用户重定向到最新的omniauth.source或stored_location,最后一种情况是根URL。

我以为我被要求创建该方法,但Devise已经做到了。

资料来源: https : //github.com/plataformatec/devise/wiki/How-To : -redirect-to-a-specific-page-on-successful-sign-in

从Devise 4开始 ,它对我无缝工作:

只要您使用devise的store_location_for(resource)存储当前页面的位置,设计就会自动重定向登录和注册。 为此,请在app/controllers/application_controller.rb编辑ApplicationController 。 加:

 # saves the location before loading each page so we can return to the # right page. If we're on a devise page, we don't want to store that as the # place to return to (for example, we don't want to return to the sign in page # after signing in), which is what the :unless prevents before_filter :store_current_location, :unless => :devise_controller? private # override the devise helper to store the current location so we can # redirect to it after loggin in or out. This override makes signing in # and signing up work automatically. def store_current_location store_location_for(:user, request.url) end 

将以下内容添加到ApplicationController以进行注销重定向:

 private # override the devise method for where to go after signing out because theirs # always goes to the root path. Because devise uses a session variable and # the session is destroyed on log out, we need to use request.referrer # root_path is there as a backup def after_sign_out_path_for(resource) request.referrer || root_path end 

正如官方文档中所指出的, 更简单的解决方案是将其添加到application_controller.rb

 class ApplicationController < ActionController::Base private # If your model is called User def after_sign_in_path_for(resource) session["user_return_to"] || root_path end 

重要说明(我也忽略了)是为了使其工作,您需要调用authenticate_user! 默认情况下在Devise中可用的方法,在你的控制器的before_action: 。 这将调用store_location_for 在Devise中开箱即用 ,其余部分由application_controller.rb的上述代码处理,因此无需重写代码来保存请求URL。

如果您的登录表单具有自己的页面,则此处的某些其他解决方案可能不起作用,而不是例如每页的标题中的登录表单。 登录后,用户需要返回页,而不仅仅是一页。

在登录,注销,更新后 ,Devise有一个很好的如何重定向到当前页面,下面的代码来自哪里。

在会话中存储原始URL是最佳选择。 除了解决上述返回页的问题之外,“许多浏览器不发送[ request.referer ]标头。因此,实现此function的唯一强大的跨浏览器方式是使用会话。”

在会话中存储URL时,重要的是不要存储任何POST,PUT或DELETE请求的URL,也不要存储任何XHR请求,即用户实际无法实际重定向的任何内容。

请注意,注销后,用户的会话将被销毁,因此存储的URL将消失。 在这种情况下,可以将用户发送回request.referer 。 这似乎是可以接受的,因为大多数网站在每个页面上都有一个注销链接,因此返回推荐人实际上会有效。

 class ApplicationController < ActionController::Base before_action :store_user_location!, if: :storable_location? before_action :authenticate_user! private def storable_location? request.get? && is_navigational_format? && !devise_controller? && !request.xhr? end def store_user_location! store_location_for(:user, request.fullpath) end def after_sign_in_path_for(resource_or_scope) stored_location_for(resource_or_scope) || super end def after_sign_out_path_for(resource_or_scope) request.referrer || super end end 

我有同样的疑问并发现了这一点,也尝试过

 def after_sign_in_path_for(resource_or_scope) session.fetch 'user_return_to', admin_root_path end 

gem 'devise', '~> 4.4.0'

  1. 创建sessions_controller.rb: class SessionsController < Devise::SessionsController

  2. 添加以下内容(如果您的登录URL不是/ users,则修改正则表达式)

     def before_login session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/ end def after_login session[:previous_url] || root_path end 

请注意,如果您在/ users下有/ users / dashboard或其他位置,则此方法无效。 可能希望通过正则表达式获得更具体的信息。