Search code examples
active-model-serializersrails-api

how to fix "Rendered ActiveModel::Serializer::Null with Hash"


I am trying to write API for user model, where i have to return only two columns with some modification(appending string)
Every thing work's fine, I even get the correct result, but when I see status code its showing '500', when i check the logs its showed the following error

[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash

following is the code
1. users_controller.rb

class Api::V1::UsersController < Api::V1::ApiController

  # GET
  def pl_details
    render json: {pl: current_user.pl_url, enabled: current_user.personal_calendar_enabled}, status: :success
  end
...
end
  1. user.rb
...
def pl_url
  return "#{Rails.application.secrets.app_host}/#{self.calendar_url_token}"
end
...
  1. user_serializer.rb
class UserSerializer < ActiveModel::Serializer
  attributes :id, :firstname, :lastname, :email
end

Solution

  • Never mind,
    I just did it other way around,I used a separate Serializer to avoid the error, following is the approach

    class Api::V1::UsersController < Api::V1::ApiController
    
      # GET
      def pl_details
        render json: current_user,serializer: PLSerializer, status: :success
      end
    ...
    end
    

    and inside PLSerializer

    class PLSerializer < ActiveModel::Serializer
      attributes :pl, :personal_calendar_enabled
    
      def personal_link
        current_user.pl_url
      end
    end