Rails 5.2 some controller actions gives InvalidAuthenticityToken

烈酒焚心 提交于 2019-12-05 20:57:36
lulalala

For Rails before 5.2, the generated ApplicationController will call protect_from_forgery, meaning POST,PUT,DELETE actions are checked for authenticity.

New Rails 5.2 projects will by default check authenticity token for any subclass of ActionController::Base instead, which affects many existing Gems.

You can wait for the gem updates for compatibility with 5.2.

Alternatively, you can probably monkey patch these controllers in the initializer:

require 'foo_controller'
class FooController < ActionController::Base
  skip_before_action :verify_authenticity_token, raise: false
end

I ran into this issue too using destroy_user_session_path in sign out link. I compared my older app (rails 5.1.x) vs this newly built rails 5.2.0, I noticed that I didn't have csrf_meta_tags in tag of my layout.

After adding

= csrf_meta_tags

it worked! HTH

ruby '2.3.5' gem 'rails', '~> 5.2.0'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!