小编Nab*_*oui的帖子

Rails ActiveModel Serializers呈现非null属性

我想使用一个渲染非null属性的序列化程序

     class PersonSerializer < ActiveModel::Serializer
        attributes :id, :name, :phone, :address, :email
     end
Run Code Online (Sandbox Code Playgroud)

这可能吗.

非常感谢.

解:

 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
Run Code Online (Sandbox Code Playgroud)

serialization attributes ruby-on-rails notnull

10
推荐指数
3
解决办法
6561
查看次数