1

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your reply.. thanks a lottt.. so haaapppyyyyyyyyyyyy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.