Created
December 17, 2009 18:20
-
-
Save documentcloud/258919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convenience method for responding with JSON. Sets the content type, | |
# serializes, and allows empty responses. If json'ing an ActiveRecord object, | |
# and the object has errors on it, a 409 Conflict will be returned with a | |
# list of error messages. | |
def json(obj, status=200) | |
obj = {} if obj.nil? | |
if obj.respond_to?(:errors) && obj.errors.any? | |
obj = {'errors' => obj.errors.full_messages} | |
status = 409 | |
end | |
render :json => obj, :status => status | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment