【问题标题】:Rendering paginated pages in JBuilder views在 JBuilder 视图中呈现分页页面
【发布时间】:2020-11-14 01:48:59
【问题描述】:

我目前正在对包含 9000 多个项目的查询参与者的返回进行分页。我的页面和路由工作正常,但我希望它们显示在页面底部,作为结果页面的可点击链接。我在使用 JBuilder 方面相对较新 我正在使用 Kaminari gem 以及 API-Pagination gem,并且想知道如何根据 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 %>

【问题讨论】:

    标签: ruby-on-rails api pagination kaminari jbuilder


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 1970-01-01
      • 2018-10-20
      • 2019-05-02
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      相关资源
      最近更新 更多