Rails 活动模型序列化器 - JSON Api
Rails Active Model Serializers - JSON Api
我正在使用 AMS 版本 0.10 并希望使用 json-api 规范来呈现我的响应。但是,我很难为我的关系数据呈现 'included' 键。我有以下设置:
products_controller.rb
class Api::V1::ProductsController < ApplicationController
...
respond_to :json
def show
respond_with Product.find(params[:id])
end
...
product_serializer.rb
class ProductSerializer < ActiveModel::Serializer
attributes :id, :title, :price, :published
has_one :user
end
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :auth_token, :created_at, :updated_at
end
products_controller_spec.rb
before(:each) do
@product = FactoryGirl.create :product
get :show, params: { id: @product.id }
end
...
it "has the user as a embeded object" do
product_response = json_response
puts "&&&&&&&&&&&&&&&"
puts product_response #output below
#expect(product_response[:user][:email]).to eql @product.user.email
end
...
json_response
{:data=>{:id=>"1", :type=>"products", :attributes=>{..working..}, :relationships=>{:user=>{:data=>{:id=>"1", :type=>"users"}}}}}
我想知道如何获取嵌套资源的 'included' 部分。
示例(来自 http://jsonapi.org/format/#introduction)
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "http://example.com/articles/1"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
},
我以前从未使用过 AMS,因此非常感谢您的帮助。
非常感谢
解决方案就在这里 https://github.com/rails-api/active_model_serializers/blob/master/docs/jsonapi/schema.md。
基本上我将以下内容添加到我的控制器操作中 (GET products/1)
render json: product, include: params[:include]
这将允许请求系统通过为要处理的 api 添加参数 include='user' 来确定他们是否想要包含嵌套模型。
谢谢
我正在使用 AMS 版本 0.10 并希望使用 json-api 规范来呈现我的响应。但是,我很难为我的关系数据呈现 'included' 键。我有以下设置:
products_controller.rb
class Api::V1::ProductsController < ApplicationController
...
respond_to :json
def show
respond_with Product.find(params[:id])
end
...
product_serializer.rb
class ProductSerializer < ActiveModel::Serializer
attributes :id, :title, :price, :published
has_one :user
end
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :auth_token, :created_at, :updated_at
end
products_controller_spec.rb
before(:each) do
@product = FactoryGirl.create :product
get :show, params: { id: @product.id }
end
...
it "has the user as a embeded object" do
product_response = json_response
puts "&&&&&&&&&&&&&&&"
puts product_response #output below
#expect(product_response[:user][:email]).to eql @product.user.email
end
...
json_response
{:data=>{:id=>"1", :type=>"products", :attributes=>{..working..}, :relationships=>{:user=>{:data=>{:id=>"1", :type=>"users"}}}}}
我想知道如何获取嵌套资源的 'included' 部分。
示例(来自 http://jsonapi.org/format/#introduction)
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "http://example.com/articles/1"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
},
我以前从未使用过 AMS,因此非常感谢您的帮助。
非常感谢
解决方案就在这里 https://github.com/rails-api/active_model_serializers/blob/master/docs/jsonapi/schema.md。
基本上我将以下内容添加到我的控制器操作中 (GET products/1)
render json: product, include: params[:include]
这将允许请求系统通过为要处理的 api 添加参数 include='user' 来确定他们是否想要包含嵌套模型。
谢谢