我想使用呈现非空属性的序列化程序
class PersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :address, :email
end
这可能吗。
非常感谢。
解决方案:
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