Reputation: 1199
I want to display all programmes which I got from a query as json response. I'm getting the programmes, but don't know how to render them through json. I'm using the jbuilder
gem and created a create.json.buider.rb
file. In my query I'm getting everything correctly, but I'm not receiving a JSON response with whatever details in I have in the query.
This is my controller. I have tried it like this but I'm not getting a json response. Only a status as 200.
class Api::V1::Categories::ProgrammesController < ApiController
respond_to :json
def category
@category=Category.all
@programmes=Programme.joins(:category).find_by(category_id: params[:category_id])
if @programmes.present?
render :json=> {:message=>"Programme not exists "}, :status=>422
else
render :json => @programmes
end
end
end
My create.json.jbuilder
file:
json.programmes @programmes
Upvotes: 1
Views: 1573
Reputation: 1545
I think you should change @programmes
to { :programmers => @programmes.as_json }
class Api::V1::Categories::ProgrammesController < ApiController
def category
@category = Category.all
@programmes = Programme.joins(:category).find_by(category_id: params[:category_id])
if @programmes.present?
render :json=> {:message=>"Programme not exists "}, :status=>422
else
render :json => { :programmers => @programmes.as_json }
end
end
end
Upvotes: 2