How to use will_paginate with a nested resource in Rails?

依然范特西╮ 提交于 2019-12-01 08:43:26

Solved -

I moved the invoices pagination into Statement's controller, like this:

def show
  @statement = Statement.find(params[:id])

  #TODO move the :per_page stuff out to a constant
  @invoices = @statement.invoices.paginate :per_page => 10,
    :page => params[:page],
    :order => 'created_at DESC'


 respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @statement }
 end
end

and call it in the view like this (code trimmed for readability>

  <div id="pagination">
  <%= will_paginate @invoices %>
  </div>
  <table>
  <%# @statement.invoices.each do |invoice| -
  shows all invoices with no pagination,
  use @invoices instead%>
  <%
  @invoices.each do |invoice|
  %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!