2017-06-22 3 views
1

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 
+0

Essayez de changer ':' location' à: location_id' dans les attributs – Pavan

+0

Pas de chance , il inclut correctement ': location_id' mais inclut toujours le hash' location' qui est l'endroit où se produit l'erreur –

Répondre

1

Tout ce que je devais faire était d'ajouter un attribut type au hachage de l'emplacement. Selon le type JSONAPI.org doit être dans le format suivant:

{ 
    "data": { 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     // ... this article's attributes 
    }, 
    "relationships": { 
     // ... this article's relationships 
    } 
    } 
} 

donc je modifié mon LocationSerializer à ce qui suit:

class LocationSerializer < ActiveModel::Serializer 
    attributes :id, :phone, :address, :name, :type 

    def type 
    return "location" 
    end 
end