2017-06-22 69 views
1

我有一個Rails API,我試圖拉入Ember中的記錄,並且在工作時,我的嵌套模型不是。我有一個Employeebelongs_to一個Location並創造像這樣一個串行:Rails + Ember:嵌套模型格式不正確

class API::EmployeeSerializer < ActiveModel::Serializer 
    attributes :id, :name, :phone, :email, :manager, :terminated, :location 
    belongs_to :location 
end 

,輸出:

{"employee": 
    {"id":19,"name":"John Abreu","phone":"","email":"","manager":false,"terminated":false,"location": 
     {"name":"Peabody","id":2} 
    } 
} 

和我的灰燼應用程序通過拉這樣的:

進口灰燼從'餘燼';

export default Ember.Route.extend({ 
    model() { 
     return this.store.findAll('employee') 
    } 
}); 

,但我打我的錯誤,當我遇到哈希location部分。我得到以下內容:

> 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. 

我該如何解決這個問題?我已經有一個LocationSerializer有:

class LocationSerializer < ActiveModel::Serializer 
    attributes :id, :phone, :address, :name 
end 
+0

嘗試改變':location'到':location_id'在屬性 – Pavan

+0

沒有運氣它正確地包含了':location_id',但仍然包含錯誤發生位置的'location'哈希 –

回答

1

所有我需要做的就是添加一個type屬性的位置哈希值。據JSONAPI.org類型應採用以下格式:

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

所以我修改了我的LocationSerializer以下幾點:

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

    def type 
    return "location" 
    end 
end