具有自定义属性的Activemodel序列化器

时间:2020-04-25 19:32:01

标签: ruby-on-rails

我必须使用这样的结构将json返回到我的前端

{
    "meta": {
        "page": 1,
        "pages": 1,
        "perpage": -1,
        "total": 40,
        "sort": "asc",
        "field": "RecordID"
    },
    "data": [
        {
            "RecordID": 1,
            "OrderID": "61715-075",
            "Country": "China",
            "ShipCountry": "CN",
            "ShipCity": "Tieba",
            "ShipName": "Collins, Dibbert and Hoeger",
            "ShipAddress": "746 Pine View Junction",
            "CompanyEmail": "nsailor0@livejournal.com",
            "CompanyAgent": "Nixie Sailor",
            "CompanyName": "Gleichner, Ziemann and Gutkowski",
            "Currency": "CNY",
            "Notes": "imperdiet nullam orci pede venenatis non sodales sed tincidunt eu felis fusce posuere felis sed lacus morbi",
            "Department": "Outdoors",
            "Website": "irs.gov",
            "Latitude": 35.0032213,
            "Longitude": 102.913526,
            "ShipDate": "2/12/2018",
            "PaymentDate": "2016-04-27 23:53:15",
            "TimeZone": "Asia/Chongqing",
            "TotalPayment": "$246154.65",
            "Status": 3,
            "Type": 2,
            "Actions": null
        },
        {
            "RecordID": 2,
            "OrderID": "63629-4697",
            "Country": "Indonesia",
            "ShipCountry": "ID",
            "ShipCity": "Cihaur",
            "ShipName": "Prosacco-Breitenberg",
            "ShipAddress": "01652 Fulton Trail",
            "CompanyEmail": "egiraldez1@seattletimes.com",
            "CompanyAgent": "Emelita Giraldez",
            "CompanyName": "Rosenbaum-Reichel",
            "Currency": "IDR",
            "Notes": "adipiscing elit proin risus praesent lectus vestibulum quam sapien varius ut blandit non interdum",
            "Department": "Toys",
            "Website": "ameblo.jp",
            "Latitude": -7.1221059,
            "Longitude": 106.5701927,
            "ShipDate": "8/6/2017",
            "PaymentDate": "2017-11-13 14:37:22",
            "TimeZone": "Asia/Jakarta",
            "TotalPayment": "$795849.41",
            "Status": 6,
            "Type": 3,
            "Actions": null
        },
        {....}
       ]
     }

我在Netflix json库中使用序列化器。 我知道如何退货:

render json: ContentSerializer.new(@contents).serializable_hash 

向我返回数据收集,但是我不明白如何添加'meta'属性

0 个答案:

没有答案