dyserone dyserone - 6 months ago 10
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?
end
end


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?

Thanks.

Answer

Create a hash and return all together. like

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

 render json: data_json
Comments