【问题标题】:How Do I Authenticate to ActiveResource to Avoid the InvalidAuthenticityToken Response?如何对 ActiveResource 进行身份验证以避免 InvalidAuthenticityToken 响应?
【发布时间】:2008-09-29 18:45:03
【问题描述】:

我正常创建了一个 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

【问题讨论】:

    标签: ruby-on-rails ruby activeresource


    【解决方案1】:

    我找到了这个问题的答案,因为我正在编写一个命令行应用程序。我在控制器中添加了以下内容:

      # you can disable csrf protection on controller-by-controller basis:
      skip_before_filter :verify_authenticity_token
    

    【讨论】:

      【解决方案2】:

      Rails 仅在您请求 html 时才需要这个,如果您正在请求 xml(可能是 html 以外的任何内容?)它不会检查那个。看起来您的服务器的销毁操作需要一个 xml 响应,问题应该会消失。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多