I'm currently building an API using Rails. All the responses are in JSON. As an example, my controllers use:
render json: @users, status: :ok
or, when I set respond_to :json
, I use:
respond_with @users, status: :ok
I want to know what is the best way of standarizing my responses. Like:
{
error_code: 0,
content: ... (@users as JSON)
}
I've tried using JBuilder and also serializers (https://github.com/rails-api/active_model_serializers), but I have to add the template to each view/serializer.
I want an elegant way of doing this so any controller that calls render
or respond_with
has the same template.
to_json
and theas_json
method can be customized to remap or omit fields. - tadman