加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用Devise before_action:authenticate_user

发布时间:2020-12-16 21:12:35 所属栏目:百科 来源:网络整理
导读:我想要在我的Rails 4网站上的所有页面上登录. 在ApplicationController中,我添加了before_action:authenticate_user!,但它根本不做任何事情.我试图添加相同的before_action:authenticate_user!到另一个控制器,它工作正常. 我是否需要对ApplicationContro
我想要在我的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控制器;他们只是按要求运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读