0

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?

nullnullnull
  • 8,039
  • 12
  • 55
  • 107

1 Answers1

5

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"}}
alexspeller
  • 788
  • 7
  • 9
  • Thanks, Alex. You have no idea how much time I've spent trying to figure that out. For others, in case `to_h` is undefined for you, try `to_hash`. Not sure why it was failing for me, it's suppose to be supported in Ruby 2.0. – nullnullnull Apr 14 '14 at 17:29