Skip to content

Instantly share code, notes, and snippets.

@paydro
Created June 22, 2009 22:49
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 paydro/134240 to your computer and use it in GitHub Desktop.
Save paydro/134240 to your computer and use it in GitHub Desktop.
#######################################################
# Add this to app/controllers/application_controller.rb
#######################################################
def render_json_response(type, hash)
unless [ :ok, :redirect, :error ].include?(type)
raise "Invalid json response type: #{type}"
end
# To keep the structure consistent, we'll build the json
# structure with the default properties.
#
# This will also help other developers understand what
# is returned by the server by looking at this method.
default_json_structure = {
:status => type,
:html => nil,
:message => nil,
:to => nil }.merge(hash)
render_options = {:json => default_json_structure}
render_options[:status] = 400 if type == :error
render(render_options)
end
#################
# Usage Examples:
#################
# Success scenario with html
format.jsonr do
render_json_response :ok, :html => "<b>Hello, world!</b>", :message => "Ajax response succeeded!"
end
# Success with a redirect
format.jsonr do
render_json_response :redirect, :to => "http://www.paydrotalks.com"
end
# Failure scenario
format.jsonr do
render_json_response :error, :message => "Oops!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment