简体   繁体   中英

Add metadata to :attributes serializer (Active Model Serializers)

Is there a way I can add a non-model metadata attribute onto an each_serializer , without using the :json_api adapter?

render json: invoices, each_serializer: Invoices::ItemizedSerializer

I want it to be...

{
  data: [{..invoice 1...}, {...invoice 2...}]
  metadata: {total: 500}
}

instead of

[
  {..invoice 1...},
  {...invoice 2...}
]

您可以基于每个控制器传递自定义适配器:

render json: invoices, adapter: :json, each_serializer: Invoices::ItemizedSerializer, meta: {grand_total: grand_total}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM