0
我正在使用Rails 5和AMS 10.我的問題以粗體顯示。ActiveModelSerializer,我如何獲得更多信息關於關聯的JSON API響應?
gem 'active_model_serializers', '~> 0.10.0.rc1'
rails -v
Rails 5.0.0.beta3
我正在使用JSON API規範。我有這個在配置/初始化/ active_model_serializer.rb文件:
ActiveModel::Serializer.config.adapter = :json_api
是這點適用公約已在節點的定製完全自由JSON API的反應?這樣可以更快地瞭解使用相似前端Ember應用程序或移動本機應用程序的API的開發人員的數據?
這是我RentalUnitSerializer:
class RentalUnitSerializer < ActiveModel::Serializer
attributes :id, :rooms, :bathrooms, :price, :price_cents
belongs_to :user
end
這是我UserSerializer:
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
def name
names = object.name.split(" ")
"#{names[0].first}. #{names[1]}"
end
end
這是我rental_units_controller:
class RentalUnitsController < ApplicationController
before_action :set_rental_unit, only: [:show, :update, :destroy]
# GET /rental_units
def index
@rental_units = RentalUnit.all
render json: @rental_units
end
這是我的JSON響應時,我打了/ rental_units端點。
{
"data": [{
"id": "1",
"type": "rental_units",
"attributes": {
"rooms": 2,
"bathrooms": 2,
"price": null,
"price_cents": 50000
},
"relationships": {
"user": {
"data": {
"id": "1",
"type": "users"
}
}
}
}, {
"id": "2",
"type": "rental_units",
"attributes": {
"rooms": 2,
"bathrooms": 2,
"price": null,
"price_cents": 50000
},
"relationships": {
"user": {
"data": {
"id": "1",
"type": "users"
}
}
}
},
我如何在JSON API響應的關係部分獲得用戶的名字?