A
A
Anthony Brand2015-10-24 17:23:59
Ruby on Rails
Anthony Brand, 2015-10-24 17:23:59

How to set json response format from API on Rails 4?

You need to set the response format from the API in this form:

{
"errorCode": 0,
"errorMessage": "",
"result": [...]
}

At the moment, I wrote a helper in application_helper, to which I give either an error code and a message, or data.
Used in .jbuilder templates
def envelope(json, error_code=0, error_message='')
    json.errorCode error_code
    json.errorMessage error_message
    json.set! :result, [1] do
        yield if block_given?
    end
  end

Is there a better way, and if so, which one?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question