Ruby 在 Rails 上:Netflix fast_jsonapi 不呈现它定义的属性

Ruby On Rails: Netflix fast_jsonapi not render it defined attributes

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

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

它不会出现,输出将return与模型相关的所有字段。

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 文档遵循了正确的说明。

大家有什么建议吗?非常感谢任何帮助:)

看起来像使用 Fast JSON API 作为 ActiveModelSerializersDocumentation 没有任何关于升级 ActiveModel 的参考。所以,render json: License.all, status: :ok 不会唤起 LicenseSerializer.

下一步试试:

render json: LicenseSerializer.new(License.all).serializable_hash, status: :ok