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' }
有人知道答案吗?
检查你的ApplicationController
是否打电话给protect_from_forgery
如下:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
基本上调用protect_from_forgery
将verify_authenticity_token
添加到before_filter
列表中,您可以跳过其他控制器。
有关更多信息,请参阅protect_from_forgery
documentation。
升级到Rails 5后,我遇到了这个错误。我发现如果使用相同的方法名称多次调用skip_before_action
,则会引发此异常。
我的修复是添加参数raise: false
,第二次调用skip_before_filter
。
所以它在应用程序控制器中看起来像这样:
skip_before_action :your_method_name, raise: false
升级到Rails 5并部署到Heroku后,我遇到了这个错误。我无法在开发中重现它。
在API docs中给出了一个例子
class ApplicationController < ActionController::Base
protect_from_forgery unless: -> { request.format.json? }
end
对我来说,添加unless: -> { request.format.json? }
像上面修复我的堆栈炸弹。遗憾的是,raise: false
没有。