I have a Rails app that has both HTML and JSON endpoints, and the JSON is being used by an iOS app. The iOS developer would like all error messages output to be changed from something like {"errors":{"email":["has already been taken"],"password":["is too short (minimum is 6 characters)"]}}
to the form { "error" : "message"}
, but there doesn't seem to be a good place to do a single fix/formatting.
My feeling is there are 3 different steps here:
- Modifying error responses for JSON responses only
- Calling
.full_messages
on all errors - Standardizing keys so
errors
or other variations become simplyerror
My questions:
- Is there a place to set the error format to
full_messages
for all JSON messages or does it need to be spliced into all error calls? - Is there a place to standardize keys for all errors?
- I guess I could reopen
as_json
inActiveRecord::Base
, but there may be other errors not coming from AR...
- I guess I could reopen
Potential items
These are potential solutions that have been mentioned so far.
- Presenters
- This seems useful for aggregating information from multiple models but still requires that the new Class be called in each method.
- I was hoping for a higher-level solution that caught output and reformatted it before passing it along without having to manually insert it throughout the app.