如何对ActiveResource进行身份验证以避免InvalidAuthenticityToken响应?
时间:2020-03-06 14:53:24 来源:igfitidea点击:
我通常创建一个Rails应用程序。然后为事件类创建支架。然后尝试下面的代码。运行时,它会在执行destroy方法时抱怨InvalidAuthenticityToken。如何进行身份验证以避免此响应?
require 'rubygems' require 'activeresource' class Event < ActiveResource::Base self.site = "http://localhost:3000" end e = Event.create( :name => "Shortest Event Ever!", :starts_at => 1.second.ago, :capacity => 25, :price => 10.00) e.destroy
解决方案
自从编写命令行应用程序以来,我找到了解决此问题的答案。我将以下内容添加到我的控制器中:
# you can disable csrf protection on controller-by-controller basis: skip_before_filter :verify_authenticity_token
Rails仅在请求html时才需要这样做,如果我们请求xml(可能是html以外的其他东西),它不会进行检查。看来我们服务器的destroy动作需要xml响应,问题应该消失了。