【发布时间】:2017-11-30 04:31:25
【问题描述】:
AMS 版本:0.9.7
我正在尝试将参数传递给 ActiveModel 序列化程序,但没有任何运气。
我的(精简的)控制器:
class V1::WatchlistsController < ApplicationController
def index
currency = params[:currency]
@watchlists = Watchlist.belongs_to_user(current_user)
render json: @watchlists, each_serializer: WatchlistOnlySerializer
end
我的序列化器:
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
我正在尝试将参数 currency 从控制器传递到序列化程序,以便在 market_value 方法中使用(在示例中被硬编码为“usd”)。
我已经尝试过@options 和@instance_options,但我似乎无法让它发挥作用。不确定它是否只是语法问题。
【问题讨论】:
标签: ruby-on-rails active-model-serializers