I want to use a serializer that renders not null attributes
class PersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :address, :email
end
Is this possible.
Many thanks.
Solution:
class PersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :address, :email
def attributes
hash = super
hash.each {|key, value|
if value.nil?
hash.delete(key)
end
}
hash
end
end
Please add validation presence:true in your Person model for (:id, :name, :phone, :address, :email) attributes, so you will get not null JSON value while you render.