I'm playing around with this 'CloudEdit' Backbone tutorial http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/, which allows you to create a "document" with "title" and "body" properties. It uses a Rails backend saving the data to the 'documents' url, and creates/edits thoughs documents through an EditView. To make a long story short, even though the success callback in the EditView on the this.model.save()is triggered on every save, and even though the console.log() messages in the view show that the 'title' and 'body' property are set with the values entered on the form, when I check the database, most of the records have 'null' for title and body, while only a few actually have the values that were entered. This pattern continues as I try to add more records, without about 4/5 showing null in the database. Based on the code below, can you see a reason why that might be, and how I might fix it?
Note, the database shows the title and body properties are null for most of the records i.e. this is not an issue of backbone not retrieving the data properly. It's not being saved properly. Model
window.Document = Backbone.Model.extend({
url : function() {
var base = 'documents';
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
}
});
View var EditView = Backbone.View.extend({ events: { "submit form": "save" },
initialize: function() {
this.render();
},
save: function() {
var self = this;
var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";
var tit = this.$('[name=title]').val();
console.log(tit);
var bod = this.$('[name=body]').val();
console.log(bod);
this.model.save({ title: this.$('[name=title]').val(), body: this.$('[name=body]').val() }, {
success: function(model, resp) {
// new App.Views.Notice({ message: msg });
console.log("save");
self.model = model;
self.delegateEvents();
},
error: function(r) {
console.log(r);
}
});
return false;
},
render: function() {
...template rendering code...
}
});
Update responding to comments of
This is the create action of the documents_controller.rb
respond_to :json
...code ommitted...
def create
puts params
respond_with Document.create(:document => params[:document])
end
However, this is what the puts params reveals, there's a warning about mass assigning protected attributes. However, 'document' isn't even an attribute of the model.
Processing by DocumentsController#create as JSON
Parameters: {"title"=>"hop", "body"=>"hot", "document"=>{"title"=>"hop", "body"=>"hot"}}
WARNING: Can't mass-assign protected attributes: document
(0.1ms) begin transaction
SQL (80.4ms) INSERT INTO "documents" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["body", nil], ["created_at", Fri, 25 Jan 2013 23:20:51 UTC +00:00], ["title", nil], ["updated_at", Fri, 25 Jan 2013 23:20:51 UTC +00:00]]
(7.3ms) commit transaction
Completed 201 Created in 127ms (Views: 31.9ms | ActiveRecord: 0.0ms)
rewrote the create action like this based on the following form inputs
def create
respond_with Document.create(:title => params[:title], :body => params[:body])
end
#form input names
<input name='title' type='text'/>
<textarea name='body'>