【问题标题】:Will Pagiante gem is limiting count results in loop to the per page number, how to avoid?Will_paginate gem 将循环中的计数结果限制为每页数,如何避免?
【发布时间】:2019-05-20 13:46:41
【问题描述】:

我在我的控制器中这样做:

@orders_e = Order.all.where(seller: current_user).where(order_status: [2] ).paginate(:page => params[:month_orders_page_1], :per_page => 12)
@orders_month = @orders_e.all.group_by { |mon|  mon.created_at.beginning_of_month }

然后在我看来:

<% @orders_month.each do |month, orders| %>
     <td><%= month.strftime("%b.") %></td>
     <td class="center"><%= orders.count %></td>
<% end %>

结果上限为 12。例如,如果当月有 20 个@orders_e,则它只会计数到 12 标记。

当我将分页代码从控制器代码的末尾取出时,它不会这样做,但前端会产生无限的结果。

我可以将循环限制在 12 个,但我希望每页有 12 个结果。

我可以通过什么方式实现每页 12 个带有分页功能的结果,无论是否使用 will_paginate gem?

(我标记了 javascript,以防有办法通过 javascript 来实现。)

【问题讨论】:

  • 不清楚你在问什么。您想对未分组的@orders_e@orders_month 内的分组订单进行分页吗?

标签: javascript ruby-on-rails ruby will-paginate


【解决方案1】:

您需要使用 2 个单独的请求来显示总数。

# controller code
scope = Order.where(seller: current_user, order_status: [2] )

@orders_e = scope.paginate(page: params[:month_orders_page_1], per_page: 12)
@order_counts = scope.group("DATE_PART('month', created_at)").count

# view code
<% @order_counts.each do |month, count| %>
  <td><%= Date::MONTHNAMES[month.to_i] %></td>
 <td class="center"><%= count %></td>
<% end %>

【讨论】:

    猜你喜欢
    • 2014-06-24
    • 2012-04-27
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多