如何将 API 响应封装在 "data" 键中并对其进行分页?
How to encapsulating API response inside "data" keys and paginate them?
我正在使用活动模型序列化程序来序列化用户。
在我的卖家控制器中我有,
def index
seller = User.all.where(catagories: "seller")
render json: seller, status: :ok, status: :ok
end
UserSerializer
class 看起来像这样。
attributes :id, :name, :email, :phone_number, :verified
has_many :products
has_one :address`
输出也还算可以。
{
"users": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"products": [
{
"id": 1,
"name": "Direct Performance Transmitter",
"price": 1000,
"warranty": false,
"discount": 50,
"description": "Vel doloribus distinctio nihil rerum libero. Reprehenderit ratione cumque porro nesciunt. Id recusandae aut vel voluptatem aperiam hic deleniti voluptas."
}
],
"address": {
"id": 1,
"latitude": 41.022921,
"longitude": -118.064638782714,
"street_name": "Beattystr.",
"city": "Hermannport"
}
}
但我需要将该响应封装在一个名为 data
的密钥中。
data : {
"users": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"products": [
{
"id": 1,
"name": "Direct Performance Transmitter",
"price": 1000,
"warranty": false,
"discount": 50,
"description": "Vel doloribus distinctio nihil rerum libero. Reprehenderit ratione cumque porro nesciunt. Id recusandae aut vel voluptatem aperiam hic deleniti voluptas."
}
],
"address": {
"id": 1,
"latitude": 41.022921,
"longitude": -118.064638782714,
"street_name": "Beattystr.",
"city": "Hermannport"
}
}
我可以通过使用 data
键将数据包装在散列中来实现,如下所示
所以我的索引方法看起来像这样。
def index
seller = User.all.where(catagories: "seller")
render json: { data: { user: seller } }, status: :ok
end
但是在这样做之后,关系不会在 json 中呈现。
{
"data": {
"user": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"avatar": "https://robohash.org/voluptatemsintnon.png?size=300x300",
"badge": 3,
"catagories": "seller",
"rating": 1,
"created_at": "2017-06-25T10:31:39.575Z",
"updated_at": "2017-06-25T10:31:39.575Z"
}
我还使用 gem 'api-pagination'
对这个数组进行分页,但是
用户被封装在数据中后,api-pagination 也停止工作。
如何获得所需的输出并对其进行分页?
这里有两种方法可以随心所欲:
render json: User.all, adapter: :json_api
或
# in config/am_serializer.rb
ActiveModelSerializers.config.adapter = :json_api
# in controller
render json: User.all
现在响应将呈现为
{
"data": {
"id": "1",
"type": "profile",
"attributes": {
"name": "Julia"
}
}
}
json_api
适配器的设计符合此处定义的规范 http://jsonapi.org/
but that will change the response format to jsonapi. the response format of all api will be different. client app does not follow jsonapi standards
试试这个,它应该有效
render json: {data: ActiveModelSerializers::SerializableResource.new(User.all)}
默认适配器是 json
而不是 json_api
我正在使用活动模型序列化程序来序列化用户。
在我的卖家控制器中我有,
def index
seller = User.all.where(catagories: "seller")
render json: seller, status: :ok, status: :ok
end
UserSerializer
class 看起来像这样。
attributes :id, :name, :email, :phone_number, :verified
has_many :products
has_one :address`
输出也还算可以。
{
"users": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"products": [
{
"id": 1,
"name": "Direct Performance Transmitter",
"price": 1000,
"warranty": false,
"discount": 50,
"description": "Vel doloribus distinctio nihil rerum libero. Reprehenderit ratione cumque porro nesciunt. Id recusandae aut vel voluptatem aperiam hic deleniti voluptas."
}
],
"address": {
"id": 1,
"latitude": 41.022921,
"longitude": -118.064638782714,
"street_name": "Beattystr.",
"city": "Hermannport"
}
}
但我需要将该响应封装在一个名为 data
的密钥中。
data : {
"users": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"products": [
{
"id": 1,
"name": "Direct Performance Transmitter",
"price": 1000,
"warranty": false,
"discount": 50,
"description": "Vel doloribus distinctio nihil rerum libero. Reprehenderit ratione cumque porro nesciunt. Id recusandae aut vel voluptatem aperiam hic deleniti voluptas."
}
],
"address": {
"id": 1,
"latitude": 41.022921,
"longitude": -118.064638782714,
"street_name": "Beattystr.",
"city": "Hermannport"
}
}
我可以通过使用 data
键将数据包装在散列中来实现,如下所示
所以我的索引方法看起来像这样。
def index
seller = User.all.where(catagories: "seller")
render json: { data: { user: seller } }, status: :ok
end
但是在这样做之后,关系不会在 json 中呈现。
{
"data": {
"user": [
{
"id": 1,
"name": "Craig Crist",
"email": "adena_kiehn@greenfelder.com",
"phone_number": "876.862.8760 x0096",
"verified": true,
"avatar": "https://robohash.org/voluptatemsintnon.png?size=300x300",
"badge": 3,
"catagories": "seller",
"rating": 1,
"created_at": "2017-06-25T10:31:39.575Z",
"updated_at": "2017-06-25T10:31:39.575Z"
}
我还使用 gem 'api-pagination'
对这个数组进行分页,但是
用户被封装在数据中后,api-pagination 也停止工作。
如何获得所需的输出并对其进行分页?
这里有两种方法可以随心所欲:
render json: User.all, adapter: :json_api
或
# in config/am_serializer.rb
ActiveModelSerializers.config.adapter = :json_api
# in controller
render json: User.all
现在响应将呈现为
{
"data": {
"id": "1",
"type": "profile",
"attributes": {
"name": "Julia"
}
}
}
json_api
适配器的设计符合此处定义的规范 http://jsonapi.org/
but that will change the response format to jsonapi. the response format of all api will be different. client app does not follow jsonapi standards
试试这个,它应该有效
render json: {data: ActiveModelSerializers::SerializableResource.new(User.all)}
默认适配器是 json
而不是 json_api