Will_paginate -error- undefined method `total_pages'

I am using will_paginate "2.3.15" for my rails application

in my 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>

my rails version 3.0.0 and ruby โ€‹โ€‹version 1.8.7

Help plz

+5
source share
2 answers

Why are you adding :all? From the will_paginate wiki, you should probably use:

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

This happened to me when the selected set size was zero. (i.e. in this case, @ units.size == 0). Testing for him in the view seemed to solve the problem. i.e.

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

All Articles