Rails:分页:如何对自定义呈现的内容进行分页

时间:2015-09-28 13:37:35

标签: ruby-on-rails pagination

我试图对一个巨大的Rails页面进行分页。这是我尝试渲染的部分代码。

<% @dated_incidents.each do |date, incidents| %>
    <div class="incident-group-date"><%= date.strftime('%e %B %Y') %></div>
    <% if incidents.is_a? ActiveRecord::Relation %>
        <% incidents.each do |i| %>
            <div class="incident-block">
              <span class="incident-name text-<%= i.convert_to_level %>"><%= i.name %></span><span class="incident-component text-<%= i.convert_to_level %>"><%= i.component %></span>
              <% e = i.events.last %>
              <hr class="event-hr"/>
              <p class="incident-description"><b><%= e.status %></b>- &nbsp;<%= e.message %></p>

              <p class="incident-updated-at"><%= e.updated_at.strftime('%b %e, %H:%M %Z') %></p>


              <% if signed_in? %>
                  <a href="/incidents/<%= i.id %>">
                    <button class="btn btn-warning btn-sm">Update Incident</button>
                  </a>
                  <a href="/incidents/delete/<%= i.id %>" data-confirm="Are you sure? Deleting an incident is irreversible.">
                    <button class="btn btn-danger btn-sm">Delete Incident</button>
                  </a>
              <% end %>
            </div>
        <% end %>
        <hr class="hr-top" />
    <% else %>
        <div class="incident-block nothing text-center"><%= incidents %></div>
        <hr class="hr-top" />
    <% end %>
<% end %>

此方法提供@dated_incidents实例变量。

  def dated_incidents
    # Returns a hash containing dates and the incidents that happened on that date
    # Sample output
    # {Sat, 26 Sep 2015=>#<ActiveRecord::Relation [#<Incident id: 980190979, name: "Incident Name", component: "Incident...>>}
    # Says 'nothing to report' if there are no incidents on that day
    # This is a bit heavy, especially if Statusify has been around for some time.
    begins = Incident.first.created_at.to_date
    ends = Incident.last.updated_at.to_date
    # Minor check to make sure things don't blow up
    begins,ends = ends,begins if begins > ends
    # The range over which we operate
    range = begins..ends
    # Runs only if @dated_incidents == nil
    if !@dated_incidents
      @dated_incidents = Hash.new
      range.each do |date|
        i = Incident.where(:created_at => date.beginning_of_day..date.end_of_day)
        if !i.empty?
          @dated_incidents[date] = i
        else
          @dated_incidents[date] = 'Nothing to report'
        end
      end
    end
    @dated_incidents.sort{|a,b| b <=> a}.to_h
  end

我希望对此输出进行分页,但我无法使用Kaminari或will_paginate执行此操作。我可以使用任何一种宝石来解决问题。 我很抱歉抛弃了这么多代码,但坦率地说,我一无所知。

0 个答案:

没有答案