4
votes

I'm trying to send an object from Backbone.js to my Rails backend. Currently when I call save() on my model, it sends this to the server:

{"program_id":1,"issuer_id":4}

But Rails is expecting it in the following format:

{"program_issuer_link":{"program_id":1,"issuer_id":4}}

Is there any way I can do this encapsulation to the JSON object that gets sent from Backbone.js to Rails when I call save() on my model? I've looked through the documentation but couldn't find anything about it.

1

1 Answers

6
votes

if you are using the backbone-rails gem, then you can do it like this.

var User = Backbone.Model.extend({
   paramRoot: 'user'
});

You can also override the toJSON method like this

var User = Backbone.Model.extend({
  toJSON: function(){
    return {user: _.clone(this.attributes)}
  },
});