Ruby on rails - 更新 ajax 的 PUT 方法 [英] Ruby on rails - PUT method on update ajax

查看:23
本文介绍了Ruby on rails - 更新 ajax 的 PUT 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么这种 PUT 方法不起作用.

Could someone tell me why this PUT method doesn't work please.

$.ajax({
        type: "PUT",
        dataType: "script",
        url: '/resources/35',
        data:
        {
            resource : { pos_y: 45, pos_x: 50 }
        }
    }).done(function( msg )
            {
                alert( "Data Saved: " + msg );
            });

服务器说我使用了GET方法,但在我的ajax请求中我输入:PUT"

Server says that I have used the GET method but in my ajax request I have type: "PUT"

Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_=1344001820350" for 192.168.1.89 at 2012-08-03 15:50:20 +0200
Processing by ResourcesController#show as */*
  Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344001820350", "id"=>"35"}
  User Load (0.3ms)  SELECT "users".* FROM "users" 
  Resource Load (0.1ms)  SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1  [["id", "35"]]
  Rendered resources/show.html.erb within layouts/login (2.3ms)
Completed 200 OK in 238ms (Views: 235.9ms | ActiveRecord: 0.4ms)

resources_controller.rb

resources_controller.rb

 # PUT /resources/1                                                                                                                                                             
  # PUT /resources/1.json                                                                                                                                                        
  def update
    @resource = Resource.find(params[:id])

    respond_to do |format|
      if @resource.update_attributes(params[:resource])
        format.html { redirect_to @resource, notice: 'successfully updated.' }
        format.js
      else
        format.html { render action: "edit" }
        format.js
      end
    end
  end

我尝试添加 _method: 'put' 但还是一样

I have tried adding _method: 'put' But it's still the same

$.ajax({
        type: "PUT",
        dataType: "script",
        url: '/resources/35',
        data:
        {
            resource : { pos_y: 45, pos_x: 50 },
            _method: 'put'
        }
    }).done(function( msg )
            {
                alert( "Data Saved: " + msg );
            });

服务器:

resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&method=put&=1344004390840"

"resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&method=put&=1344004390840"

Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_method=put&_=1344004390840" for 192.168.1.89 at 2012-08-03 16:33:10 +0200
Processing by ResourcesController#show as */*
  Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344004390840", "id"=>"35"}
  User Load (0.3ms)  SELECT "users".* FROM "users" 
  Resource Load (0.1ms)  SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1  [["id", "35"]]
  Rendered resources/show.html.erb within layouts/login (0.8ms)
Completed 200 OK in 93ms (Views: 90.5ms | ActiveRecord: 0.4ms)

感谢您的帮助.

推荐答案

Rails 通过参数 _method 的值为 'put' 来确定 put 请求.

Rails determines the put request via the parameter _method with the value 'put'.

由于并非所有浏览器都支持 put 方法,因此 rails 在 form_tag 中作弊.它将这个输出放在一个 PUT 形式中:

As not all the browsers support the put method, rails cheats in the form_tag. its putting this output in a PUT form:

<input type='hidden' name='_method' value='put'>

所以你要做的是:

$.ajax({
    type: "POST",
    dataType: "script",
    url: '/resources/35',
    contentType: 'application/json',
    data: JSON.stringify({ resource:{pos_y:45,pos_x:50}, _method:'put' })
}).done(function( msg )
        {
            alert( "Data Saved: " + msg );
        });

这篇关于Ruby on rails - 更新 ajax 的 PUT 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆