dyserone dyserone - 1 year ago 75
JSON Question

Rails, a way to render everything my app has in one json?

I want to render everything my app has to json, so I can pass it to android studio.
I have a general controller and a method initialize and I want to pass everything from there.

class ApiController < ApplicationController
def initialize

@articles = Article.all
@areas = Area.all
@languages = Language.all
#now what?

I know it's probably a starter's question but I can't find a simple example to see how this works. Or I'm not searching right. Do I need a gem, or I can do that with *render :json => @data * or something similar?


Answer Source

Create a hash and return all together. like

 data_json = { articles: @articles, areas: @areas, languages: @languages }

 render json: data_json
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download