1

目前我正在使用fast_jsonapi gem来处理 API 请求而不是 AMS。

但是当我想从中删除某些字段attributes或添加自定义字段时。

它不会出现,并且输出将返回与模型相关的所有字段。

licenses_controller.rb

class Api::V1::LicensesController < ApiController
  api :GET, "/v1/licenses", "get Listing licenses"
  formats ['json']
  def index
     render json: License.all, status: :ok
  end
end

license_serializer.rb

 class LicenseSerializer
  include FastJsonapi::ObjectSerializer
  attributes :code, :module
end

返回的输出

[  
   {  
      "id":6011,
      "code":"TBI-DA",
      "module":"Data Accessing",
      "amount":"1 lisensi server",
      "serial_number":"serial_6011",
      "created_at":"2018-07-30T11:13:22.002+07:00",
      "updated_at":"2018-07-30T11:13:22.002+07:00"
   },
   {  
      "id":6012,
      "code":"TBI-DAP",
      "module":"Dashboard Analytical Processing",
      "amount":"200 lisensi pengguna",
      "serial_number":"serial_6012",
      "created_at":"2018-07-30T11:13:22.018+07:00",
      "updated_at":"2018-07-30T11:13:22.018+07:00"
   }
]

预期输出:

[  
   {  
      "id":6011,
      "code":"TBI-DA",
      "module":"Data Accessing"
   },
   {  
      "id":6012,
      "code":"TBI-DAP",
      "module":"Dashboard Analytical Processing"
   }
]

我错过了一些配置吗?我想,我已经根据 gem 文档遵循了正确的指令。

有什么建议吗?我真的很感激任何帮助:)

4

1 回答 1

2

看起来像使用Fast JSON APIas ActiveModelSerializers文档没有任何关于升级的参考ActiveModel。所以,render json: License.all, status: :ok不唤起LicenseSerializer

接下来试试:

render json: LicenseSerializer.new(License.all).serializable_hash, status: :ok
于 2018-07-30T07:09:25.090 回答