使用Devise before_action:authenticate_user!不做任何事情 [英] Using Devise before_action :authenticate_user! doesn't do anything

查看:236
本文介绍了使用Devise before_action:authenticate_user!不做任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails 4网站上的所有页面上登录。
在ApplicationController中,我添加了 before_action:authenticate_user!,但它根本没有做任何事情。我试图添加相同的 before_action:authenticate_user!到另一个控制器,它的工作正常。

I am trying to require login on all pages on my Rails 4 web site. In the ApplicationController I have added before_action :authenticate_user!, but it simply doesn't do anything. I have tried to add the same before_action :authenticate_user! to another controller, and it works fine.

需要为ApplicationController做别的事情,使所有操作都需要登录(除了注册/登录)?

Do I need to do something else to the ApplicationController, to make the login be required on all actions (Except signup/signin)?

推荐答案

这是我们使用的实际代码:

Here's the actual code we use:

#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
   #Actions
   before_action :authenticate_user! #-> routes to the login / signup if not authenticated
end

你可能有的问题是两个-fold:

The problem you probably have is two-fold:

-

确保您的其他控制器从 application_controller

Make sure your other controllers are inheriting from application_controller:

#app/controllers/other_controller.rb
Class OtherController < ApplicationController 
    ...
end

-

您以某种方式跳过 before_action 回调

You're somehow "skipping" the before_action callback

如果您在任何地方使用 skip_before_action ,则需要将其删除。这可能会导致您的 authenticate_user!方法出现问题。为了解决这个问题,我将首先测试没有任何 skip_before_action 回调,然后看看它是否正常工作

If you're using skip_before_action anywhere, you need to remove it. It will likely cause a problem with your authenticate_user! method. To fix this, I would firstly test without any skip_before_action callbacks, and then see what gets it working correctly

另外一个注释 - 登录/注册不重要。他们都继承自 Devise 控制器;他们将根据需要运行。

A further note - signin / signup won't matter. They all inherit from the Devise controllers; they'll just run as required.

这篇关于使用Devise before_action:authenticate_user!不做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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