Saving nested objects with Rails, backbone.js, and accepts_nested_attributes_for

后端 未结 4 1389
心在旅途
心在旅途 2020-12-07 23:30

I\'m using Rails, backbone.js (learning this now). Let\'s say you have two models, Car and Engine.

var Car = Backbone.Model.extend({
  initialize: function(         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 23:57

    I believe we've to do something with (or around) Backbone sync or with rails backend, because the problem in communication between two. Overriding toJSON() method might lead to unexpected results, because this is general purpose method and others parts of application might rely on, for example views.

    Possibly quick solution:

    redCar.save({}, {
        contentType: 'application/json',
        data: JSON.stringify({car: redCar.toJSON(), engines_attributes: redCar.get('engines').toJSON()})
    });
    

提交回复
热议问题