使用 Devise before_action :authenticate_user!不做任何事

2023-12-28

我试图要求登录我的 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回调,然后看看是什么让它正常工作


进一步说明 -signin / signup没关系。他们都继承自Devise控制器;他们只会按要求运行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Devise before_action :authenticate_user!不做任何事 的相关文章

随机推荐