will_paginate -error-undefined method `total_pages'

笑着哭i 提交于 2019-12-10 13:27:07

问题


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


回答1:


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')



回答2:


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 %> 


来源:https://stackoverflow.com/questions/6356062/will-paginate-error-undefined-method-total-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!