Jwan622
Jwan622

Reputation: 11639

Inspecting the JSON response before sending the response in Rails

I need to inspect the JSON response before sending it out via the controller. The Serializer attaches some fields to the response that I need to inspect which will determine if I fire an event in a separate thread/send an email. How do I inspect the JSON response before sending it out via the controller?

Here's what I have in my Rails controller:

respond_to do |format|
  format.any(*Version.formats) do
    render json: stuffs, each_serializer: StuffsSerializer
  end
end

The above just renders json and sends it immediately whereas I need an intermediate step to just inspect stuffs after it has been serialized. I prefer to do this in a line above the respond_to block. How can I do this?

Upvotes: 2

Views: 86

Answers (1)

Roman Kovtunenko
Roman Kovtunenko

Reputation: 447

You can do something like:

stuffs_json = ActiveModel::ArraySerializer.new(stuffs, each_serializer: StuffsSerializer)

Then you can inspect this variable and just put it inside render

Upvotes: 1

Related Questions