Nan*_*ini 3 serialization ruby-on-rails json-api ruby-on-rails-5.2 fastjsonapi
我有一个控制器方法drop_down_values,在其中选择一组值并以 json 响应,使用序列化器构建对象。我正在使用 FastJson Api。我想知道,如何访问序列化器中控制器中存在的其他变量。
def drop_down_values
@drop_down_values = IndustrySectorList.all
@values = @drop_down_values.pluck(:industry_sector)
render json: DropDownValueSerializer.new(@drop_down_values).serialized_json
end
Run Code Online (Sandbox Code Playgroud)
我需要@values在序列化器中使用该变量。我如何将其传递给序列化器?我无法在序列化器中直接访问此变量。
序列化器:
class DropDownValueSerializer
include FastJsonapi::ObjectSerializer
attributes :id
end
Run Code Online (Sandbox Code Playgroud)
堆栈和版本 -
我不太确定你想做什么,但序列化器通常是一种资源。在您的情况下,您可以将参数传递到下拉序列化器中:
DropDownValueSerializer.new(movie, {params: {values: @values}})
Run Code Online (Sandbox Code Playgroud)
class DropDownValueSerializer
include FastJsonapi::ObjectSerializer
attributes :id
attribute :values do |drop_down_value, params|
params[:values]
end
end
Run Code Online (Sandbox Code Playgroud)