[英]How Do I Authenticate to ActiveResource to Avoid the InvalidAuthenticityToken Response?
I created a Rails application normally. 我通常创建一个Rails应用程序。 Then created the scaffold for an event class.
然后为事件类创建支架。 Then tried the following code.
然后尝试下面的代码。 When run it complains about a InvalidAuthenticityToken when the destroy method is executed.
运行时,它会在销毁方法执行时抱怨InvalidAuthenticityToken。 How do I authenticate to avoid this response?
如何进行身份验证以避免此响应?
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
I found an answer to this issue which works since I am writing a command-line application. 我找到了解决此问题的答案,因为我正在编写命令行应用程序。 I added the following to my controller:
我在控制器中添加了以下内容:
# you can disable csrf protection on controller-by-controller basis:
skip_before_filter :verify_authenticity_token
Rails only requires this when you're requesting html, if you're requesting xml (possibly anything other than html?) it doesn't check for that. Rails仅在请求html时需要此功能,如果您请求xml(可能是html以外的其他内容),则不会进行检查。 Looks like the destroy action for your server needs an xml response and the problem should go away.
看来您服务器的destroy动作需要xml响应,问题应该消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.