Skip to content

Instantly share code, notes, and snippets.

@raedatoui
Created August 1, 2012 21:10
Show Gist options
  • Save raedatoui/3230784 to your computer and use it in GitHub Desktop.
Save raedatoui/3230784 to your computer and use it in GitHub Desktop.
json responses
def render_json_response(type, hash)
unless [ :ok, :error ].include?(type)
raise "Invalid json response type: #{type}"
end
default_json_structure = {
:status => type,
:data => nil,
:error => nil,
:message => nil}.merge(hash)
render_options = {:json => default_json_structure.reject{|k,v| v.nil?}}
render_options[:status] = 400 if type == :error
render(render_options)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment