Add pagination to include method in json responseAsk Question

问题:

I am new to Rails.

How to add pagination to a method in json?

I have a show method as below:

 def show
  respond_to do |format|
    format.html { redirect_to @question, notice: 'Y'}
    format.json {render json: @question.to_json( :include => { :responses => {:methods => [:description, :file_url]  } } )}
    end
 end

I want to paginate responses included in questions.to_json. How do I do that?

Thanks!!

回答1:


You just need to add gem, will_paginate ref: https://github.com/mislav/will_paginate

 def show
   @questions = Question.all.paginate(page: params[:page], per_page: 4)
   respond_to do |format|
    format.html { redirect_to @question, notice: 'Y'}
    format.json {render json: @questions.to_json( :include => { :responses => {:methods => [:description, :file_url]  } } )}
  end
end
标签: pagination
© 2014 TuiCode, Inc.