【问题标题】:will_paginate -error-undefined method `total_pages'will_paginate -error-undefined 方法`total_pages'
【发布时间】:2011-09-15 09:52:04
【问题描述】:

我正在为我的 rails 应用程序使用 will_paginate "2.3.15"

在我的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

请帮忙

【问题讨论】:

    标签: ruby-on-rails will-paginate


    【解决方案1】:

    当所选的集大小为零时,我会发生这种情况。 (即在这种情况下,@units.size == 0)在视图中对其进行测试似乎可以解决问题。即。

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢 Adrien 的快速回复,我使用此 @units = Unit.paginate(:page => params[:page], :order => 'created_at DESC') 仍然遇到同样的错误
      • 我会想到这个版本的 will_paginate 中的一个错误,但我不确定。也许您可以尝试更新到较新的版本。
      猜你喜欢
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多