0
votes

I'm trying to destroy a model (both in a collection and on my server), and from what I understand, backbone.js should pass the model.id and a DELETE request to my server if I call

this.model.destroy();

when I view console.log(this.model);, the console shows my model with this structure.

d
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
cid: "c2"
collection: d
id: 13
__proto__: o

rails returns a 404 error, and when I output the response from backbone destroy function, I get

Object
abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this}
always: function (){return b.done.apply(b,arguments).fail.apply(this,arguments)}
complete: function (){if(!d){var c=arguments,g,h,i,j,k;b&&(k=b,b=0);for(g=0,h=c.length;g↵↵↵  ↵  Action Controller: Exception caught↵  ↵    body { background-color: #fff; color: #333; }↵↵    body, p, ol, ul, td {↵      font-family: helvetica, verdana, arial, sans-serif;↵      font-size:   13px;↵      line-height: 18px;↵    }↵↵    pre {↵      background-color: #eee;↵      padding: 10px;↵      font-size: 11px;↵    }↵↵    a { color: #000; }↵    a:visited { color: #666; }↵    a:hover { color: #fff; background-color:#000; }↵  ↵↵↵↵

Routing Error

No route matches [DELETE] "/menu_dishes"

↵↵↵↵↵↵" setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this} status: 404 statusCode: function (a){if(a){var b;if(s

Any idea on how I should be deleting my model? I don't see anything in the documentation about how I can pass the model id through to rails, as I think that is what is missing.

1
this.destroy() doesn't work because you are in a view, there's no "destroy()" on a view. this.model.destroy() would be the correct way (documentcloud.github.com/backbone/#Model-destroy) though you should give us the content (console.log(this.model)), that would help. And use a success/error callback in your call to this.model.destroy(), so you have more info on what happens. - mna
And what routes are defined on your server? I don't know Ruby on Rails that much, but I think it uses a "_method" field to simulate "delete" and "put" http requests, right? Backbone supports this using Backbone.emulateHTTP = true;. Maybe worth a try. - mna

1 Answers

1
votes

If you are using the default Backbone sync, then the url used for a model is calculated by this method (from Backbone source):

url: function() {
  var base = getValue(this.collection, 'url') || getValue(this, 'urlRoot') || urlError();
  if (this.isNew()) return base;
  return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
}

You are getting "/menu_dishes" when you think you should be getting "/menu_dishes/the_id", right?

You can add a breakpoint to Backbone.sync and step through the above code. Maybe the id is not set right? I am just guessing at this point, but by default Backbone is expecting the id attribute to be 'id', and maybe that is not the name of your id attribute (maybe it is 'dishId' or something?).

If that is the case, check out the Backbone source:

idAttribute: 'id'

Maybe you need to override that in your model.