sites
sites

Reputation: 21805

Whitespaces not present when rendering JSON errors in Rails

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?

Update

I forgot to write {errors: ...}, so I passing hash to render json: call.

Upvotes: 2

Views: 234

Answers (2)

house9
house9

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

Webdev
Webdev

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

Related Questions