will_paginate -error-undefined method`total_pages'

时间:2011-06-15 10:06:49

标签: ruby-on-rails will-paginate

我正在使用will_paginate“2.3.15”作为我的rails app

在我的units_controller.rb

def index
    @units = Unit.paginate(:all ,:page => params[:page], :order => 'created_at DESC')
end

in my views(index)

        <%= will_paginate(@units)%>

but it gives error

undefined method `total_pages' for #<ActiveRecord::Relation:0xb523dc>

我的rails版本3.0.0  和ruby版本1.8.7

plz help

2 个答案:

答案 0 :(得分:4)

为什么要添加:all? 你应该使用will_paginate wiki

@units = Unit.paginate(:page => params[:page], :order => 'created_at DESC')

答案 1 :(得分:0)

当选定的设置大小为零时,我就会发生这种情况。 (即在这种情况下,@ units.size == 0)在视图中对它进行测试似乎解决了这个问题。即

<% if @units.size > 0 %>
  <%= will_paginate @units %>
<% end %>