使用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 

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

确保您的其他控制器inheritance自application_controller

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

-

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

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


进一步说明 - signin / signup无关紧要。 它们都inheritance自Devise控制器; 他们只是按要求运行。