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

ruby-on-rails – ActionController :: InvalidAuthenticityToke

发布时间:2020-12-16 21:17:09 所属栏目:百科 来源:网络整理
导读:背景细节 我正在使用Devise进行身份验证以登录到Rails 5应用程序. 每当我捆绑Audited或Paper Trail gem时,当我尝试#create一个新会话时(通过登录表单 – / users / sign_in),我收到以下错误: ActionController::InvalidAuthenticityToken 环境细节 Ruby 2.3
背景细节

我正在使用Devise进行身份验证以登录到Rails 5应用程序.

每当我捆绑Audited或Paper Trail gem时,当我尝试#create一个新会话时(通过登录表单 – / users / sign_in),我收到以下错误:

ActionController::InvalidAuthenticityToken

环境细节

Ruby 2.3.1

宝石:

> rails 5.0.2
> devise => 4.2.1
> paper_trail => 7.0.1

重现步骤:

>创建Rails 5应用程序
>添加Devise gem
>添加Audited或Paper Trail gem
>尝试登录

解决方法

事实证明,Devise documentation对于这个错误非常有启发性:

For Rails 5,note that protect_from_forgery is no longer prepended to
the before_action chain,so if you have set authenticate_user before
protect_from_forgery,your request will result in “Can’t verify CSRF
token authenticity.
To resolve this,either change the order in which
you call them,or use protect_from_forgery prepend: true
.

修复是从我的应用程序控制器中更改代码:

protect_from_forgery with: :exception

对此:

protect_from_forgery prepend: true

在我尝试添加Audited或Paper Trail宝石之前,这个问题并没有表现出来.

(编辑:李大同)

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

    推荐文章
      热点阅读