这是什么意思 ?>> ActionController :: InvalidAuthenticityToken

Tri*_*rip 1 ruby jquery ruby-on-rails

我很好奇这意味着什么.

但这里有具体细节..

我正在做一个可排序的jquery项目,它涉及这个rails操作:

def update_order
  params[:media].each_with_index do |id, index|
    media = @organization.media.find(id)
    media.do_not_touch = true
    media.update_attribute('position', index+1)
  end if params[:media]
  render :nothing => true
end
Run Code Online (Sandbox Code Playgroud)

我只是想找出这个错误出现的一般原因.

ven*_*les 5

Rails 在提交数据时自动检查伪造数据.来自doc:

通过确保所有表单来自当前Web应用程序,而不是来自其他站点的伪造链接,通过嵌入基于存储在会话中的随机字符串(攻击者不会知道)的令牌来保护控制器操作免受CSRF攻击)以Rails生成的所有形式和Ajax请求,然后在控制器中验证该令牌的真实性

您可以为给定的Ajax调用禁用此功能,或者您也可以发送名为"authenticity_token"的参数,其值为 <%= form_authenticity_token %>

要禁用它(我不建议),您可以执行以下操作之一:

class FooController < ApplicationController
  protect_from_forgery :except => :update_order

  # you can disable csrf protection on controller-by-controller basis:
  skip_before_filter :verify_authenticity_token
end
Run Code Online (Sandbox Code Playgroud)