gpt4 book ai didi

ruby-on-rails - ActionController::InvalidAuthenticityToken Rails 5/Devise/Audited/PaperTrail gem

转载 作者:数据小太阳 更新时间:2023-10-29 06:45:14 25 4
gpt4 key购买 nike

背景详情

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

每当我捆绑AuditedPaper Trail gem 时,当我尝试#create 一个新 session 时(通过登录表单 -/users/sign_in),我收到以下错误:

ActionController::InvalidAuthenticityToken

环境详情

ruby 2.3.1

gem :

  • rails 5.0.2
  • 设计 => 4.2.1
  • paper_trail => 7.0.1

重现步骤:

  1. 创建 Rails 5 应用程序
  2. 添加 Devise gem
  3. 添加经过审计或纸质记录的 gem
  4. 尝试登录

最佳答案

事实证明,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.

解决方法是更改​​我的应用程序 Controller 中的代码:

 protect_from_forgery with: :exception

对此:

 protect_from_forgery prepend: true

在我尝试添加 Audited 或 Paper Trail gem 之前,这个问题并没有出现。

关于ruby-on-rails - ActionController::InvalidAuthenticityToken Rails 5/Devise/Audited/PaperTrail gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com