[英]ActiveModel Serializer - Passing params to serializers
AMS version: 0.9.7 AMS版本:0.9.7
I am trying to pass a parameter to an ActiveModel serializer without any luck. 我试图将参数传递给ActiveModel序列化器,没有任何运气。
My (condensed) controller: 我的(精简)控制器:
class V1::WatchlistsController < ApplicationController
def index
currency = params[:currency]
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists, each_serializer: WatchlistOnlySerializer
end
My serializer: 我的序列化器:
class V1::WatchlistOnlySerializer < ActiveModel::Serializer
attributes :id, :name, :created_at, :market_value
attributes :id
def filter(keys)
keys = {} if object.active == false
keys
end
private
def market_value
# this is where I'm trying to pass the parameter
currency = "usd"
Balance.watchlist_market_value(self.id, currency)
end
I am trying to pass a parameter currency
from the controller to the serializer to be used in the market_value
method (which in the example is hard-coded as "usd". 我试图将参数
currency
从控制器传递到序列化器,以便在market_value
方法中使用(在该示例中,硬编码为“usd”)。
I've tried @options and @instance_options but I cant seem to get it work. 我试过@options和@instance_options但我似乎无法让它工作。 Not sure if its just a syntax issue.
不确定它只是一个语法问题。
AMS version: 0.10.6 AMS版本:0.10.6
Any options passed to render
that are not reserved for the adapter
are available in the serializer as instance_options
. 传递给
render
任何未为adapter
保留的选项在序列化程序中可用作instance_options
。
In your controller: 在你的控制器中:
def index
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists, each_serializer: WatchlistOnlySerializer, currency: params[:currency]
end
Then you can access it in the serializer like so: 然后您可以在序列化程序中访问它,如下所示:
def market_value
# this is where I'm trying to pass the parameter
Balance.watchlist_market_value(self.id, instance_options[:currency])
end
Doc: Passing Arbitrary Options To A Serializer Doc:将任意选项传递给序列化程序
AMS version: 0.9.7 AMS版本:0.9.7
Unfortunately for this version of AMS, there is no clear way of sending parameters to the serializer. 不幸的是,对于这个版本的AMS,没有明确的方法将参数发送到串行器。 But you can hack this using any of the keywords like
:scope
( as Jagdeep said ) or :context
out of the following accessors : 但你可以使用任何关键字来破解它
:scope
( 如Jagdeep所说 )或:context
以下访问者中的 :context
:
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
Though I would prefer :context
over :scope
for the purpose of this question like so: 虽然我更喜欢
:context
:scope
这个问题的目的:scope
是这样的:
In your controller: 在你的控制器中:
def index
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists,
each_serializer: WatchlistOnlySerializer,
context: { currency: params[:currency] }
end
Then you can access it in the serializer like so: 然后您可以在序列化程序中访问它,如下所示:
def market_value
# this is where I'm trying to pass the parameter
Balance.watchlist_market_value(self.id, context[:currency])
end
Try using scope
in controller: 尝试在控制器中使用
scope
:
def index
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists, each_serializer: WatchlistOnlySerializer, scope: { currency: params[:currency] }
end
And in your serializer: 在你的序列化器中:
def market_value
Balance.watchlist_market_value(self.id, scope[:currency])
end
You can send your params to your serializer like this 你可以将你的参数发送到这样的序列化器
render json: @watchlists, each_serializer: WatchlistOnlySerializer, current_params: currency
and in your serializer you can use this to get the value 在您的序列化程序中,您可以使用它来获取值
serialization_options[:current_params]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.