0
votes

I have a rails app using backbone. In the console, I can create a collection, fetch documents from the server (3) which I confirm by checking length

docs = new Docs();
docs.fetch();
docs.length
3

get one of those docs

d1 = docs.at(0) Object { cid= "c3" , changed={...}, attributes={...}, more...}

destroy the doc

d1.destroy(); DELETE http: localhost:3000/docs/1
204 No Content 23ms jquery.js?body=1 (line 8215) Object { readyState= 1 , setRequestHeader=function(), getAllResponseHeaders=function(), more...}

check the length of docs

>>> docs.length

2

Create a new collection

docz = new Docs();

get the documents from the server. Note the "304 Not modified message"

docz.fetch(); GET http:// localhost:3000/docs 304 Not Modified 31ms
jquery.js?body=1 (line 8215) Object { readyState= 1 , setRequestHeader=function(), getAllResponseHeaders=function(), more...}

Check the length. It's 3, when I expected it to be 2.

>>> docz.length

3

I don't know why when I call destroy I'm getting a no content message if there's clearly 3 records

204 No Content
        23ms

When I retrieve a record from the database, I have no problem accessing data

d.get('title')
>>>"diet book"

I have a url set on the model, so I should be able to delete individual records I'd think

   url : function() {
       var base = 'docs';
       if (this.isNew()) return base;
       return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
    },

However, I also can't delete going through a collection docs.at(0).destroy has the same effect.

This is my destroy function in the rails controller

class DocsController < ApplicationController


    respond_to :json
    ....

   def destroy
     respond_with Doc.find(params[:id])
   end 
end 

The model

 class Doc < ActiveRecord::Base
  attr_accessible :keywords, :text, :title
 end

Update One person who commented on this post noted that I'm supposed to destroy something when I call destroy, but I thought calling destroy on the object destroyed the object

d.destroy()  #should destroy d, shouldn't it?

This is what I understood from the docs

 book.destroy({success: function(model, response) {
  ...
}});

Indeed, when I call save() on an object, it saves the object to the database

d.save(); #this works, so why not d.destroy();
1
sorry, forgot to include the opening paragraph. should be better now. - Leahcim
Shouldn't your destroy, well, destroy something? - mu is too short
@muistooshort i'm calling it on the object. d.destory(). Therefore destroying the object. How am I supposed to call it? when I create an individual record, I also do it on the ojbect. d.save() which works fine. Can you elaborate? - Leahcim
Your destroy controller method on the server side looks like a show since it just sends the Doc back. A destroy controller should destroy something. - mu is too short

1 Answers

0
votes

When you do this in your Backbone code:

d.destroy();

you'll trigger a DESTROY request to the server and the client-side model will be destroyed and cleaned up. Your server code will have to deal with the DESTROY request by destroying the server-side model; your destroy controller doesn't do that:

def destroy
  respond_with Doc.find(params[:id])
end

All that does is pull the appropriate Doc out of the database and sends it back to the client: your destroy controller needs to destroy the server-side model. You need to adjust your controller method to include:

doc = Doc.find(params[:id])
doc.destroy

A bit of permission checking, error checking, and exception handling on the find and destroy calls might be a good idea too.