我是Rails的新手。
如何在json中为方法添加分页?
我有一个show方法如下:
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
我想分页问题包含在questions.to_json中。我该怎么做?
谢谢!
答案 0 :(得分:0)
你只需要添加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