【发布时间】:2017-11-25 01:50:21
【问题描述】:
我有一个 Rails API,我正在尝试在 Ember 中提取记录,虽然它可以工作,但我的嵌套模型却没有。我有一个 Employee 那个 belongs_to 一个 Location 并创建了一个像这样的序列化程序:
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}
}
}
我的 ember 应用程序通过以下方式实现了这一点:
从“ember”导入 Ember;
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
【问题讨论】:
-
尝试在属性中将
:location更改为:location_id -
不走运,它正确地包含了
:location_id,但仍然包含了发生错误的location哈希
标签: ruby-on-rails serialization ember.js rails-api