Default Serializer Rendering Options in Rails Controller

I am using Active Serializers in a rails project and has an object user

that needs to be passed from controller to serializer:

# Note the 'user:' option that will be accessible inside
# the serializer with @options[:user]
def show
render json: @some_object, user: current_user
end

def index
render json: SomeObject.all, user: current_user
end

      

It's good enough for what I'm trying to do, but it's not very DRY and results in statements render

that are littered with options. When these parameters change, I need to go back and manually delete / change them in all activities.

My question is: Is there a way to set a default parameter list to be called render

at the controller level instead of manually entering parameters into each action of one controller?

+3


source to share


1 answer


This can be done by adding this method to the controller:

def default_serializer_options  
  {user: current_user}  
end

      



Then you can access it from the serializer via options[:user]

+3


source







All Articles