Ruby on rails Basic.ajax发布到rails 3.2.2脚手架生成“;警告:Can';t验证CSRF令牌的真实性”;

Ruby on rails Basic.ajax发布到rails 3.2.2脚手架生成“;警告:Can';t验证CSRF令牌的真实性”;,ruby-on-rails,jquery,Ruby On Rails,Jquery,我试图学习如何通过jquery使用$.ajax将一些数据发布到一个简单的rails脚手架项目中。有一个标准的scaffold-created-controller=>Images class ImagesController < ApplicationController # GET /images # GET /images.json def index @images = Image.all respond_to do |format| for

我试图学习如何通过jquery使用$.ajax将一些数据发布到一个简单的rails脚手架项目中。有一个标准的scaffold-created-controller=>Images

class ImagesController < ApplicationController
  # GET /images
  # GET /images.json
  def index
    @images = Image.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @images }
    end
  end

  # GET /images/1
  # GET /images/1.json 
  def show
    @image = Image.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @image }
    end
  end

  # GET /images/new
  # GET /images/new.json
  def new
    @image = Image.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @image }
    end
  end

  # GET /images/1/edit
  def edit
    @image = Image.find(params[:id])
  end

  # POST /images
  # POST /images.json
  def create
    @image = Image.new(params[:image])

    respond_to do |format|
      if @image.save
        format.html { redirect_to @image, notice: 'Image was successfully created.' }
        format.json { render json: @image, status: :created, location: @image }
      else
        format.html { render action: "new" }
        format.json { render json: @image.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /images/1
  # PUT /images/1.json
  def update
    @image = Image.find(params[:id])

    respond_to do |format|
      if @image.update_attributes(params[:image])
        format.html { redirect_to @image, notice: 'Image was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @image.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /images/1
  # DELETE /images/1.json
  def destroy
    @image = Image.find(params[:id])
    @image.destroy

    respond_to do |format|
      format.html { redirect_to images_url }
      format.json { head :no_content }
    end
  end
end
我不知道为什么名字里没有“johngalt”。这是否与“警告:无法验证CSRF令牌真实性”有关

编辑 当我使用curl时:

curl -d "image[name]=johngalt"  localhost:3000/images.json

将创建记录,并且名称字段包含“johngalt”。本质上,我正在试图找出.ajax与我在curl中所能做的事情的等价物?

当您使用Rails
form\u for
helper创建表单时,CSRF令牌会自动添加到您的post表单中,并用于保护用户免受攻击。因此,如果您试图在javascript文件中发布,您将无法访问令牌

如果愿意,您可以禁用特定操作的CSRF令牌身份验证,只要您了解其后果

有几种方法可以做到这一点,如下所示:

Edit看看您的CURL示例,看起来您是在发布错误的数据。您正在使用“image”参数命名空间。尝试:

<script>
$(document).ready(function(){
    $.ajax({
        type: 'POST', url: "localhost:3000/images",
        data: { image: { name: "johngalt" } }
    });
});
</script>

$(文档).ready(函数(){
$.ajax({
键入:“POST”,url:“localhost:3000/图像”,
数据:{image:{name:“johngalt”}
});
});

是CSRF令牌问题,阻止字段=>name中的值=>“johngalt”更新吗?您的问题是需要发布
数据:{image:{name:{johngalt}}
,就像您在允许上传字段的curl请求中所做的那样。如果发生此错误,XMLHttpRequest无法加载。访问控制不允许原点为空允许原点,围绕跨域问题旋转?听起来像这样。尝试将其插入控制器以查看发生的情况:
skip\u before\u filter:verify\u authenticity\u token
感谢您的回复,但这并没有解决问题。
curl -d "image[name]=johngalt"  localhost:3000/images.json
<script>
$(document).ready(function(){
    $.ajax({
        type: 'POST', url: "localhost:3000/images",
        data: { image: { name: "johngalt" } }
    });
});
</script>