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

后端 未结 4 1427
心在旅途
心在旅途 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:45

    I would suggest to override toJSON on the backbone model.

    toJSON: function(){
    
      json = {car : this.attributes};
      return _.extend(json, {engine_attributes: this.get("engine").toJSON());
    
    }
    

    toJSON is called within the sync method just before sending data to the backend.

提交回复
热议问题