我想在渲染JSON时覆盖Kaminari的分页,或者告诉它以分页的方式返回所有分页。
在App1中,我使用ActiveResource访问App2的Group
模型:
class Group < ActiveResource::Base
self.site = "http://www.app2.com:3000"
end
这是App2的Group
模型:
class Group < ActiveRecord::Base
default_scope order('name asc')
paginates_per 10
这是我的控制器。 Group.search
内容为ransack:
class GroupsController < ApplicationController
# GET /groups
# GET /groups.json
def index
@search = Group.search(params[:q])
@groups = @search.result.page params[:page]
respond_to do |format|
format.html # index.html.erb
format.json { render json: @groups }
end
end
我已经向App2添加了11个组。在App1的控制台中,我得到:
[45] pry(main)> Group.all.count
=> 10
在不更改HTML分页呈现的情况下,最好的方法是什么?
答案 0 :(得分:3)
您可以准备所需的所有常用逻辑,但只对HTML格式应用分页:
def index
@search = Group.search(params[:q])
@groups = @search.result
respond_to do |format|
format.html { @groups = @groups.page(params[:page]) }
format.json { render :json => @groups }
end
end
答案 1 :(得分:1)
您可以使用不同的格式运行不同的代码:
def index
respond_to do |format|
format.html {
@search = Group.search(params[:q])
@groups = @search.result.page params[:page]
} # index.html.erb
format.json {
render json: Group.all
}
end
end