Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 活动模型序列化程序:如何将选项传递给集合?_Ruby On Rails_Active Model Serializers - Fatal编程技术网

Ruby on rails 活动模型序列化程序:如何将选项传递给集合?

Ruby on rails 活动模型序列化程序:如何将选项传递给集合?,ruby-on-rails,active-model-serializers,Ruby On Rails,Active Model Serializers,如果我有一个小部件集合,比如说小部件,并且我正在使用活动模型序列化程序来序列化小部件集合,那么如何将实例_选项传递给集合 render json: @widgets, count: 40 我尝试了上述方法,但在我的实例\u选项中似乎无法获得计数:40。我遗漏了什么吗?您可以在Widgetserializer的方法中调用@instance\u options[:count] 在控制器中: render json: @widgets, count: 40 对于exmaple class Widg

如果我有一个小部件集合,比如说小部件,并且我正在使用活动模型序列化程序来序列化小部件集合,那么如何将实例_选项传递给集合

render json: @widgets, count: 40

我尝试了上述方法,但在我的
实例\u选项中似乎无法获得
计数:40
。我遗漏了什么吗?

您可以在Widgetserializer的方法中调用
@instance\u options[:count]

在控制器中:

render json: @widgets, count: 40
对于exmaple

class WidgetsSerializer < ActiveModel::Serializer
  attributes :count

  def count
    @instance_options[:count] #=> 40
  end
end
类WidgetsSerializer40 结束 结束
您可以在WidgetsSerializer的方法中调用
@instance\u options[:count]

在控制器中:

render json: @widgets, count: 40
对于exmaple

class WidgetsSerializer < ActiveModel::Serializer
  attributes :count

  def count
    @instance_options[:count] #=> 40
  end
end
类WidgetsSerializer40 结束 结束
我想你可以超过当地人<代码>呈现json:@widgets,locals:{count:40}我想你可以传递locals<代码>呈现json:@widgets,局部变量:{count:40}