问题描述
我目前正在对包含9000多个项目的查询与会者的返回进行分页。我的页面和路由工作正常,但我希望它们作为结果页面的可单击链接显示在页面底部。我在使用JBuilder方面比较陌生,正在使用Kaminari以及API-Pagination宝石,并且想知道如何根据Kaminari Docs <%= paginate @attendees %>
将可见/可单击的页码添加到JBuilder视图中。这是必需的。但是据我了解,JBuilder不能将其逻辑解释为纯粹的制造JSON对象?感谢您提供任何建议以及对JBuilder所做工作的更好解释。
Controller
module Reports
class ConferencesController < ::ApplicationController
def attendees
@conference = Conference.find(attendee_params[:conference_id])
@attendees = @conference.attendees
paginate json: @attendees,per_page: 500
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.conference @conference,partial: 'conference',as: :conference
json.attendees @attendees,partial: 'attendee',as: :attendee
<%= paginate @attendees %>
解决方法
Kaminari非常适合HTML部分使用,但是您需要做一些其他事情才能将其设置为其他响应格式。您可以从控制器中删除paginate json: @attendees,per_page: 500
行,例如使用
@attendees = @conference.attendees.page(params[:page]).per(500)
此外,您将需要向jbuilder部分提供其他信息以呈现此信息。
类似这样的东西:
json.meta do
json.total_pages @attendees.total_pages
json.total_count @attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope