背景详情
我正在使用 Devise 进行身份验证以登录到 Rails 5 应用程序。
每当我捆绑Audited 或Paper Trail gem 时,当我尝试#create 一个新 session 时(通过登录表单 -/users/sign_in),我收到以下错误:
ActionController::InvalidAuthenticityToken
环境详情
ruby 2.3.1
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.
解决方法是更改我的应用程序 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/