Rails: Pass Parameters to ActiveModel Serializer

2018-03-14 ·

In ActiveModel::Serializer version 0.10+, use @instance_options to read the parameters passed to a Active Model serializer.

Pass the parameter from an action in a controller:

class SomeController < ApplicationController
  def action
    render json: @model, your_option_name: value
  end
end

Access the parameter's value inside the corresponding ActiveModel serializer:

class ModelSerializer < ActiveModel::Serializer
  def some_method
    puts @instance_options[:your_option_name]
  end
end
Copyright © 2018 All rights reserved.
Powered by Kulfon.