Skip to content

Instantly share code, notes, and snippets.

@svisamsetty
Last active December 27, 2015 18:29
Show Gist options
  • Save svisamsetty/7370004 to your computer and use it in GitHub Desktop.
Save svisamsetty/7370004 to your computer and use it in GitHub Desktop.
Every action which we define in the controller should support and return response in JSON format unless otherwise noted. Below is the gist of how it should look.
#prototype of how an action which returns JSON should look.
def action
@result = Hash.new
@result['status'] = true
@result['error_messages'] = [ ]
@result['success_messages'] = [ ]
if <success>
@clients = Client.all
@clients_data = []
@clients.each do |client|
@single_client = Hash.new
@single_client['client_info']= client
@single_client['projects'] = client.projects
@clients_data << @single_client
end
@result['data'] = @clients_data
@result['success_messages'].push("Action performed successfully")
else
@result['status'] &= false
@result['error_messages'].push("There was an error performing this action")
end
respond_to do |format|
format.json { render json: @result }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment