2017-08-05 35 views
0

如果我有一些東西的集合,比如說Widgets,並且我正在使用Active Model Serializer來序列化Widgets集合,那麼我如何將instance_options傳遞給集合呢?活動模型序列化器:如何將選項傳遞給集合?

render json: @widgets, count: 40 

我嘗試了上面,我似乎無法得到count: 40instance_options。我錯過了什麼嗎?

+0

我想你可以通過本地人。 'render json:@widgets,locals:{count:40}' –

回答

1

您可以在WidgetsSerializer的方法中調用@instance_options[:count]

在控制器:

render json: @widgets, count: 40 

對於〔實施例,

class WidgetsSerializer < ActiveModel::Serializer 
    attributes :count 

    def count 
    @instance_options[:count] #=> 40 
    end 
end 
相關問題