You could easily achieve this error format by adding ActionController :: Responder for your JSON format. See http://api.rubyonrails.org/classes/ActionController/Responder.html for (extremely vague) documentation on this class, but in a nutshell you need to override the to_json method.
In the example below, I call a private method in ActionController: Responder, which will build a json response, including a custom error response of your choice; all you have to do is fill in the blanks, really:
def to_json json, status = response_data render :json => json, :status => status end def response_data status = options[:status] || 200 message = options[:notice] || '' data = options[:data] || [] if data.blank? && !resource.blank? if has_errors?
Nathan kleyn
source share