最佳答案
我正在开发一个新的Rails 4应用程序(在Ruby 2.0.0-p0上),这时我遇到了一些真实性令牌问题。
在编写响应json的控制器(使用respond_to
类方法)时,我得到了create
动作,当我试图使用curl
创建记录时,我开始得到ActionController::InvalidAuthenticityToken
异常。
我确保我设置了-H "Content-Type: application/json"
,我设置了-d "
的数据,但仍然没有运气。
我尝试使用Rails 3.2(在Ruby 1.9.3上)编写相同的控制器,我没有遇到任何真实性令牌问题。我四处搜索,发现Rails 4中真实性令牌有一些变化。据我所知,它们不再自动插入到表单中了?我认为这在某种程度上影响了非html内容类型。
有没有什么方法可以解决这个问题,而不需要请求HTML表单,获取真实性令牌,然后使用该令牌发出另一个请求?还是我完全忽略了一些非常明显的东西?
编辑:我刚刚尝试在一个新的Rails 4应用程序中使用脚手架创建一个新记录,没有改变任何东西,我遇到了同样的问题,所以我猜这不是我做的事情。