Rails 3: How to return errors in a JSON request? Rails 3: How to return errors in a JSON request? json json

Rails 3: How to return errors in a JSON request?


The same way you return such errors with html, it's part of the HTTP Header.

render json: @myobject, status: :unprocessable_entity

Update, response to comment:

You can get all the status codes from Rack. Rails passes the symbolized status to Rack

Rack::Utils.status_code(options[:status])

which simply matches the symbol to the list of status (the strings are converted to symbols)Here is the smoking fresh list: https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L575-L638

Scroll a bit lower and you'll see the status_code method. It's fun to read the source code!