J'ai une API Rails et j'essaie d'insérer des enregistrements dans Ember et pendant son fonctionnement, mon modèle imbriqué ne l'est pas. J'ai un Employee
qui belongs_to
un Location
et ont créé un sérialiseur comme ceci:Rails + Ember: Modèles imbriqués format incorrect
class API::EmployeeSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :email, :manager, :terminated, :location
belongs_to :location
end
qui sort:
{"employee":
{"id":19,"name":"John Abreu","phone":"","email":"","manager":false,"terminated":false,"location":
{"name":"Peabody","id":2}
}
}
et mon application Ember tire cela à travers:
importation Ember de ' braise ';
export default Ember.Route.extend({
model() {
return this.store.findAll('employee')
}
});
mais je frappe mon erreur quand je rencontre la partie location
du hachage. Je reçois ce qui suit:
> Assertion Failed: Ember Data expected the data for the location
> relationship on a <employee:19> to be in a JSON API format and include
> an `id` and `type` property but it found {name: Peabody, id: 2}.
> Please check your serializer and make sure it is serializing the
> relationship payload into a JSON API format.
Comment puis-je corriger cela? J'ai déjà un LocationSerializer
qui a:
class LocationSerializer < ActiveModel::Serializer
attributes :id, :phone, :address, :name
end
Essayez de changer ':' location' à: location_id' dans les attributs – Pavan
Pas de chance , il inclut correctement ': location_id' mais inclut toujours le hash' location' qui est l'endroit où se produit l'erreur –