ActiveModel::Serializer makes it easy to output json-api compatible models, but if you try to respond with validation errors your 422 response will not be in json-api format, which will break compatible tools (e.g., Ember adapters)
You can monkey-patch rails so that respond_with
formats validation errors correctly (since ActiveModel::Error
already has enough information to be useful in json-api).
Just add an initializer that overrides ActionController::Responder#json_resource_errors
:
# e.g in config/initializers/action_controller_json_api.rb
ActionController::Responder.class_eval do
def json_resource_errors
errors = resource.errors.map do |key, error|
{
id: key,
title: error,
status: :unprocessable_entity
}
end
{ errors: errors }
end
end