2014-09-03 73 views

回答

2

創建一個串行的新實例時,您可以解析選項Hash,但它使用的唯一屬性是:root,你可以在ActiveModel::Serializersource code看到:

def initialize(object, options = {}) 
    @object = object 
    @root = options[:root] || (self.class._root ? self.class.root_name : false) 
end 

您可以覆蓋此方法串行器類,並根據需要使用其餘選項:

class PostSerializer < ActiveModel::Serializer 
    attributes :title, :body 

    def initialize(object, options = {}) 
    super(object, options) 
    # Your custom code goes here 
    end 
end