ruby-on-rails – 使用Devise before_action:authenticate_user!什么都不做

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

我是否需要对ApplicationController执行其他操作,以使所有操作都需要登录(除了注册/登录)?

解决方法

这是我们使用的实际代码
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
   #Actions
   before_action :authenticate_user! #-> routes to the login / signup if not authenticated
end

你可能遇到的问题有两个:

确保您的其他控制器继承自application_controller:

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

你以某种方式“跳过”before_action回调

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

进一步说明 – 登录/注册无关紧要.它们都继承自Devise控制器;他们只是按要求运行.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...