Saving this model:
export default DS.Model.extend({
type: DS.attr('string'),
primary: DS.attr('boolean'),
address: DS.attr('string'),
address2: DS.attr('string'),
city: DS.attr('string'),
state: DS.attr('string'),
postal: DS.attr('string'),
customer: DS.belongsTo('customer')
});
Using this route:
3 export default Ember.Route.extend({
4 model() {
5 return this.store.createRecord('customer-address', {
6 customer: this.modelFor('customers/show')
7 });
8 },
9 actions: {
10 save() {
11 var model = this.modelFor('customer-addresses/new');
12 model.save().then(() => {
13 this.transitionTo('customer-addresses');
14 });
15 },
16 cancel() {
17 this.transitionTo('customer-addresses');
18 }
19 }
20 });
Params are sent to my backend like this:
Parameters: %{"customer_address" => %{"address" => nil, "address2" => nil, "city" => nil, "customer" => "23", "postal" => nil, "primary" => false, "state" => nil, "type" => ""}}
customer
needs to be customer_id
(a la Rails) as customer hasMany customer_addresses.
UPDATE: Here's my serializer
1 import config from '../config/environment';
2 import DS from 'ember-data';
3 import Ember from 'ember';
4
5 var serializer;
6 if (config.environment === 'test') {
7 serializer = DS.JSONSerializer.extend({});
8 } else {
9 serializer = DS.RESTSerializer.extend({
10 keyForAttribute: function(attr, method) {
11 return Ember.String.underscore(attr);
12 },
13 payloadKeyFromModelName: function(modelName) {
14 return Ember.String.underscore(modelName);
15 },
16 });
17 }
18
19 export default serializer;
You should override keyForRelationship
on your serializer (see here).
keyForRelationship: function(key, relationship, method) {
return Ember.String.underscore(key) + '_id';
}
If you are using Rails specifically I would look the Ember Data Active Model Adapter/Serializer, which has been extracted into an addon here.