Ruby On Rails: Netflix fast_jsonapi not render it defined attributes

1.6k views Asked by At

currently i'm using fast_jsonapi gem for handling API request instead AMS.

But when I want to remove some field from attributes or add custom field.

It won't appear, and the output will return all field related to the model.

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

Returned output

[  
   {  
      "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"
   }
]

Expected Output:

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

Did I miss some configuration ? I think, i've followed the right instruction based on gem documentation.

Any suggestion guys ? I'll really appreciate any kind of help :)

1

There are 1 answers

0
Leo On BEST ANSWER

Looks like using Fast JSON API as ActiveModelSerializers. Documentation hasn't any references to upgrading ActiveModel. So, render json: License.all, status: :ok not evoke LicenseSerializer.

Try next:

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