私は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を使用したことがない任意のヘルプは大歓迎されます。解決策はここにhttps://github.com/rails-api/active_model_serializers/blob/master/docs/jsonapi/schema.mdあるだけで他の誰のための
感謝