【问题标题】:How to encapsulating API response inside "data" keys and paginate them?如何将 API 响应封装在“数据”键中并对其进行分页?
【发布时间】:2017-11-28 20:51:56
【问题描述】:

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

在我的卖家控制器中,

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

如何实现所需的输出并对其进行分页?

【问题讨论】:

  • 你能添加一下你的控制器代码吗?
  • 其序列化程序的默认功能是使用 data 键包装数据并添加关系
  • 默认呈现根节点users内的所有用户信息。如上面的第一个响应所示。但我的要求需要包含在数据中的所有用户信息,如第二个响应所示。
  • 请添加您的控制器代码
  • 第一个代码是我的卖家控制器的索引方法。

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


【解决方案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

【讨论】:

  • 但这会将响应格式更改为 jsonapi。所有 api 的响应格式都会不同。客户端应用不遵循 jsonapi 标准
  • 我虽然你试图遵守
猜你喜欢
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
  • 2021-09-09
  • 1970-01-01
  • 2018-08-17
  • 2021-10-28
  • 2015-09-24
相关资源
最近更新 更多