Reputation: 21805
I have been doing this in my controllers
render json: {errors: @user.errors}, status: 422
And when I see at browser console when user has errors on saving I see something like:
{errors: {my_field: ["Anerrorhasocurred"]}}
Where should I fix this error?
I forgot to write {errors: ...}
, so I passing hash to render json:
call.
Upvotes: 2
Views: 234
Reputation: 20624
You might want to try full_messages - http://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-full_messages
render json: @user.errors.full_messages, status: 422
Should return an array of validation errors
Upvotes: 2
Reputation: 98
Are you sure the string has whitespace prior to jsonification?
Try adding a line that drops @user.errors.inspect
to the dev log along with @user.errors.to_json
, then retry the error.
Upvotes: 0