I am using Backbone.js (version 0.5.3) and am having some trouble with a success callback when saving a model. It isn’t being run, even though the model is successfully saved on the server.
CoffeeScript:
console.log 'in switch_private'
console.log "private_entry attribute is currently #{@model.get('private_entry')}"
@model.save {'private_entry': true},
success: ->
console.log 'in success'
Compiled Javascript:
console.log('in switch_private');
console.log("private_entry attribute is currently " + (this.model.get('private_entry')));
return this.model.save({
'private_entry': true
}, {
success: function() {
return console.log('in success');
}
});
Console output:
in switch_private
private_entry attribute is currently false
XHR finished loading: "http://localhost:3000/entries/235".
I am returning head :ok from the update action in Ruby on Rails.
Adding the model and response arguments, so that it’s success: (model, response) ->, doesn’t make a difference. What is going wrong?
EDIT:
As per Trevor Burnham’s suggestion, I added an error callback, and it is being run. So what should I be returning from the Ruby on Rails action in order for Backbone to consider the save a success? At the moment I have head :ok
EDIT 2: Here is my updated compiled Javascript:
var this_view;
this_view = this;
return this.model.save({
'private_entry': !(this.model.get('private_entry'))
}, {
success: function(model, response) {
return console.log('in success');
},
error: function(model, response) {
return console.log('in error');
}
});
Here is the PUT request:

I’ve run into this. You can’t just return
head :okand use Backbone’s default behavior. The default Backbone.Sync won’t have it.First of all, if you are doing it in your
createaction, you won’t ever know what youridis so the model won’t be able to update later (which you are doing, because of the “PUT”).Second, in your
updateaction, the model won’t know that the data is truly in sync if you returnhead :okso the sync fails again. But is doesn’t matter if you don’t have anid.Whatever the case, you need to return something in the body.
Rails scaffolding, by default, returns
head :okupon successfulupdate. This doesn’t jive with Backbone. To fix it, return the JSON instead:(where
@entityis whatever your variable is in the action)