Ruby on rails 如何将API响应封装在“内部”;“数据”;钥匙和页码?

Ruby on rails 如何将API响应封装在“内部”;“数据”;钥匙和页码?,ruby-on-rails,api,pagination,active-model-serializers,json-api,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 ha

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

在我的卖家控制器中我有

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`
 def index
   seller = User.all.where(catagories: "seller")
   render json:  { data: { user: seller } },  status: :ok
end
产出也相当公平

{
    "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
键将数据包装成如下所示的散列来实现这一点 所以我的索引方法是这样的

  attributes :id, :name, :email, :phone_number, :verified
  has_many :products
  has_one :address`
 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分页'
对这个数组进行分页,但是

在将用户封装到数据中之后,api分页也停止工作


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

以下是两种方法:

render json: User.all, adapter: :json_api

现在,响应将呈现为

{
  "data": {
    "id": "1",
    "type": "profile",
    "attributes": {
      "name": "Julia"
    }
  }
}
json\u api
适配器的设计符合此处定义的规范

有用的链接:



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

试试这个,应该有用

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

默认适配器是
json
而不是
json\u api

您可以添加控制器代码的预览吗?它的默认功能是序列化程序,使用
数据
键包装数据,并添加关系。默认情况下,它会呈现根节点
用户
内的所有用户信息。如上面的第一个响应所示。但是我的需求需要将所有用户信息包装在数据中,如第二个响应中所示。请添加控制器代码第一个代码是我的卖方控制器索引方法。但这会将响应格式更改为jsonapi。所有api的响应格式都将不同。客户端应用程序不遵循jsonapi标准,尽管您试图遵守