Rails和BackboneJS破坏模型骨干误解了响应

时间:2013-03-28 04:32:39

标签: ruby-on-rails rest backbone.js

我正在销毁骨干网中的模型并发送正确的DELETE请求。请求一切都很好;我甚至检查了chrome调试器,在网络下它说请求有status 200但是主干由于某种原因调用我的错误函数。这是代码:

destroy : function() {
    new App.Views.Notice({ message: "Removing reminder...", type: "message"});
    this.model.destroy({
        success: function(model, response){
            this.close();
            new App.Views.Notice({message: "Successfully remove reminder", type:"success"});
        },

        error: function() {
            new App.Views.Notice({message: "Could not remove reminder", type:"error"});
        }
    });
},

在我的Rails模型中:

def destroy
    note = Note.find_by_id(params[:id])
    note.destroy
end

(我应该在这里渲染一些json吗?)

以下是请求信息:

DELETE /notes/41 HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://127.0.0.1:3000
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31
Referer: http://127.0.0.1:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: _Notely_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTc1MTk3OGRkYzNiZGUzODY1YjYyYWM2MmNjOWZjZDViBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMXNUWGZDUkpHL1F2Um52NXB0emNsNnpmWDVVS1dmZ1RLOUtkQk9STXA2UjA9BjsARg%3D%3D--b186601186315cb3cc7aee51b92d1e2a76a8279e

回复信息:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-Ua-Compatible: IE=Edge
Cache-Control: no-cache
X-Request-Id: 94ab62df5f0b462901174ae329d37e1f
X-Runtime: 0.088005
Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-01-15)
Date: Thu, 28 Mar 2013 04:25:34 GMT
Content-Length: 0
Connection: Keep-Alive
Set-Cookie: _Notely_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTA1YWViMDI2NmZjMjI3NDM4ZDcwZjBkZjhjNWVjYzFiBjsAVA%3D%3D--507388d81c70eb937fe521749b60e7f170d15671; path=/; HttpOnly

在Chrome调试器的响应标签下,它还显示“此请求没有可用的响应数据”我应该在Rails中发送内容吗?如果是这样,是什么?

1 个答案:

答案 0 :(得分:0)

目前我正在使用BackboneJS。在正常情况下,当我们在Backbone Model中调用REST方法时,它将发送JSON请求(您可以在服务器控制台as JSON中看到)。我更喜欢使用JSON响应:

respond_to do |format|
  format.json {render :json => {:msg => "item deleted!"},:status => 200}
end 

我真的不知道问题是什么,因为它的响应状态为200。但为了解决这个问题,我将我的破坏代码粘贴在Backbone中(在咖啡脚本中)

model.destroy(
  success: (model, response, options) =>
    #do other stuff
  error: (model, response, options) =>
    #do stuff for error
)