2016-10-03 42 views
0

我正在使用AMS 0.10版,並且正在使用json-api規範來呈現我的響應。但是,我很難爲我的關係數據呈現「包含」鍵。我有以下設置:Rails活動模型序列化器 - JSON Api

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"}}}}} 

我想知道如何得到嵌套的資源 '包括' 部分。

實施例(從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。

非常感謝

回答

相關問題