Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created December 17, 2009 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save documentcloud/258919 to your computer and use it in GitHub Desktop.
Save documentcloud/258919 to your computer and use it in GitHub Desktop.
# 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