如何将API响应封装在"数据"键和分页?

时间:2017-06-26 02:49:46

标签: ruby-on-rails api pagination active-model-serializers json-api

我正在使用活动模型序列化程序来序列化用户。

在我的卖家控制器中,我有,

def index
   seller = User.all.where(catagories: "seller")
   render json: seller, status: :ok,  status: :ok
end

UserSerializer课程看起来像这样。

  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也会停止工作。

如何实现所需的输出并将其分页?

1 个答案:

答案 0 :(得分:1)

以下是两种方法:

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/

有用的链接:
https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#other

  

但这会将响应格式更改为jsonapi。所有api的响应格式都不同。客户端应用程序不遵循jsonapi标准

试试这个,它应该有用

render json: {data: ActiveModelSerializers::SerializableResource.new(User.all)}

默认适配器为json而不是json_api