Question

Unfortunately, ActiveModel::Serializers does not currently support validation errors, though they're scheduled for 1.0. Until then, I've got to hack a solution of my own. The big problem? I have no idea what format Ember Data's ActiveModelAdapter expects these errors to be in. I tried simply passing in the errors property, but Ember Data didn't pick up on it:

class MySerializer < ActiveModel::Serializer
  attributes :errors
end

So what should I pass in instead?

Was it helpful?

Solution

I use this method to render validation errors (note that you don't use the serializer at all):

def render_validation_errors errors
  render json: {errors: errors.to_h}, status: 422
end

You would use it like this:

def create
  model = Model.new model_params
  if model.save
    respond_with model
  else
    render_validation_errors model.errors
  end
end

The format expected by ActiveModelAdapter is:

{"errors":{"title":"should begin with a capital letter"}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top