小编Rac*_*hel的帖子

ActiveModel序列化器-将参数传递给序列化器

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
Run Code Online (Sandbox Code Playgroud)

我的序列化器:

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
Run Code Online (Sandbox Code Playgroud)

我正在尝试将参数currency从控制器传递给要在market_value方法中使用的序列化器(在示例中,其硬编码为“ usd”。

我已经尝试过@options和@instance_options,但似乎无法正常工作。不确定是否只是语法问题。

ruby-on-rails active-model-serializers

5
推荐指数
1
解决办法
2603
查看次数