Skip to content

Overriding Backbone.sync breaks Rails' strong parameters #188

Open
@jarrett

Description

@jarrett

Line 19 in backbone_rails_sync.js reads:

data = JSON.stringify(options.attrs || model.toJSON(options));

It looks like this variable is always overwritten in the next few lines:

if (model.paramRoot) {
  data = {};
  data[model.paramRoot] = model.toJSON(options);
} else {
  data = model.toJSON();
}

So line 19 seems to do nothing. If I'm correct, It's a few CPU cycles gone to waste.

More importantly though, options.attrs is ignored. That's bad because it breaks calls like the following:

myModelInstance.save({name: "example"}, {patch: true});

The above should send only {name: "example"} rather than the whole attributes hash in the HTTP request. This is very important in Rails, where strong parameters is the officially supported way to whitelist attributes. In Rails, you want to be able to set config.action_controller.action_on_unpermitted_parameters = :raise to catch your own mistakes, but it breaks when backbone-rails forces you to send every attribute with every request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions