Rails + Devise:如何覆盖before_filter“authenticate_user!”的重定向。 [英] Rails + Devise: How to override redirect for the before_filter "authenticate_user!"

查看:183
本文介绍了Rails + Devise:如何覆盖before_filter“authenticate_user!”的重定向。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 3和最新版本的Devise,并且在我的 AdminController 之前有一个过滤器,以 authenticate_user!我需要在 request.referrer 之前存储一个会话变量,然后再重定向,以便我们可以将它发送回/ admin页面,当他们尝试继续。我将在哪里覆盖 authenticate_user!

I am on Rails 3 and the latest version of Devise, and I have a before filter in my AdminController to authenticate_user! I need to store a session variable for the request.referrer before it redirects so that I can send it back to the /admin page when they try to go on it. Where would I overwrite authenticate_user!?

我想做的是这样的,但我不知道在哪里定义它:

What I want to do is this, but I don't know where to define it:

def authenticate_user!
  session[:return_to] = request.request_uri
  super
end


推荐答案

您实际上不需要这样做,设计将尊重 after_sign_in_path ,以达到这一目的。

You don't actually need to do that, devise will respect an after_sign_in_path for this exact purpose.

在您的应用程序控制器中:

In your application controller:

before_filter :set_return_path 

def after_sign_in_path_for(resource) 
  session["user_return_to"] || root_url 
end

def set_return_path
  unless devise_controller? || request.xhr? || !request.get?
    session["user_return_to"] = request.url
  end
end

从设计助手:

# The default url to be used after signing in. This is used by all Devise
# controllers and you can overwrite it in your ApplicationController to
# provide a custom hook for a custom resource.
# def after_sign_in_path_for(resource_or_scope)

这篇关于Rails + Devise:如何覆盖before_filter“authenticate_user!”的重定向。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆