I need to write some code in one place (base_controller.rb
which inherits from application_controller.rb
) that wraps all outgoing json responses from controllers in a json
object before returning.
In my Rails (5.x.x) application, there are ~100 endpoints serving different types of data (json objects, arrays, array of arrays etc). Due to a recent requirement, I have to wrap them all in a parent object.
Previous state:
some_controller.rb
return_obj = { key: value }
render json: return_obj
Expected state:
some_controller.rb
will return { key: value }
, but this will be intercepted by base_controller.rb
which in turn will return something like { payload: { key: value }, other_info: info }