在 Ember 模型指南中http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships我发现关联应该指定为 id 数组:
{
“邮政”: {
“评论”:[1,2,3]
}
}
我无法弄清楚如何在 Rails 控制器中生成 id 数组。虽然我可以 :include 关联的模型,但它们作为哈希数组包含在内:
{"name":"简的家","rooms":[{"id":1},{"id":2},{"id":3}]}
关于如何获得数组形式有什么想法吗?
Ember 建议使用 active_model_serializers gem 以兼容的格式生成 JSON。
这是 active_model_serializer 文档中的一个示例,几乎可以完全满足您的要求。这embed :ids
是关键。
class PostSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :title, :body
has_many :comments
end
https://github.com/rails-api/active_model_serializers https://github.com/rails-api/active_model_serializers
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)