will_paginate -error-undefined method `total_pages'
Asked Answered
C

2

6

I am using will_paginate "2.3.15" for my rails app

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

plz help

Cleodal answered 15/6, 2011 at 10:6 Comment(0)
S
4

Why do you add the :all ? From the will_paginate wiki you should probably use :

@units = Unit.paginate(:page => params[:page], :order => 'created_at DESC')
Scheming answered 15/6, 2011 at 10:17 Comment(2)
Thanks Adrien for your quick response i use this @units = Unit.paginate(:page => params[:page], :order => 'created_at DESC') still getting same errorCleodal
I would think to a bug in this version of will_paginate but I'm not sure about that. Maybe you can try to update to a newer version.Scheming
S
0

This occurred for me when the selected set size was zero. (ie. in this case, @units.size == 0) Testing for it in the view seemed to solve the problem. ie.

<% if @units.size > 0 %>
  <%= will_paginate @units %>
<% end %> 
Sankey answered 14/3, 2015 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.