verify_authenticity_token尚未定义

问题描述 投票:18回答:3

Ruby版本2.2.4,Rails版本5.0.0.1。

我被困在tutorial的一部分,你用curl测试登录。我收到一个错误

ArgumentError(在process_action回调之前:尚未定义verify_authenticity_token)。

我在sessions_controller中使用了这段代码:

skip_before_action :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }

有人知道答案吗?

ruby-on-rails ruby-on-rails-5
3个回答
19
投票

检查你的ApplicationController是否打电话给protect_from_forgery如下:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end

基本上调用protect_from_forgeryverify_authenticity_token添加到before_filter列表中,您可以跳过其他控制器。

有关更多信息,请参阅protect_from_forgery documentation


9
投票

升级到Rails 5后,我遇到了这个错误。我发现如果使用相同的方法名称多次调用skip_before_action,则会引发此异常。

我的修复是添加参数raise: false,第二次调用skip_before_filter

所以它在应用程序控制器中看起来像这样:

skip_before_action :your_method_name, raise: false

0
投票

升级到Rails 5并部署到Heroku后,我遇到了这个错误。我无法在开发中重现它。

API docs中给出了一个例子

class ApplicationController < ActionController::Base
  protect_from_forgery unless: -> { request.format.json? }
end

对我来说,添加unless: -> { request.format.json? }像上面修复我的堆栈炸弹。遗憾的是,raise: false没有。

© www.soinside.com 2019 - 2024. All rights reserved.