Ruby on rails NoMethodError-未定义的方法`总页数';对于#<;阵列:0x007fdd28be0430>;:
我的模型中有一组来自活动记录的集合Ruby on rails NoMethodError-未定义的方法`总页数';对于#<;阵列:0x007fdd28be0430>;:,ruby-on-rails,will-paginate,Ruby On Rails,Will Paginate,我的模型中有一组来自活动记录的集合 @ad_groups = AdGroup.where(is_template: false).paginate(:page => params[:page]).order(:id).reverse 然后在我的模型中 self.per_page = 25 WillPaginate.per_page = 25 在我看来,索引 = will_paginate @ad_groups 以及所需的将分页/阵列。我看到有人在初始化器中使用了这个代码 req
@ad_groups = AdGroup.where(is_template: false).paginate(:page => params[:page]).order(:id).reverse
然后在我的模型中
self.per_page = 25
WillPaginate.per_page = 25
在我看来,索引
= will_paginate @ad_groups
以及所需的将分页/阵列。我看到有人在初始化器中使用了这个代码
require 'will_paginate/collection'
class Array
# Paginates a static array (extracting a subset of it). The result is a
# WillPaginate::Collection instance, which is an array with a few more
# properties about its paginated state.
#
# Parameters:
# * <tt>:page</tt> - current page, defaults to 1
# * <tt>:per_page</tt> - limit of items per page, defaults to 30
# * <tt>:total_entries</tt> - total number of items in the array, defaults to
# <tt>array.length</tt> (obviously)
#
# Example:
# arr = ['a', 'b', 'c', 'd', 'e']
# paged = arr.paginate(:per_page => 2) #-> ['a', 'b']
# paged.total_entries #-> 5
# arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd']
# arr.paginate(:page => 3, :per_page => 2) #-> ['e']
#
# This method was originally {suggested by Desi
# McAdam}[http://www.desimcadam.com/archives/8] and later proved to be the
# most useful method of will_paginate library.
def paginate(options = {})
page = options[:page] || 1
per_page = options[:per_page] || WillPaginate.per_page
total = options[:total_entries] || self.length
WillPaginate::Collection.create(page, per_page, total) do |pager|
pager.replace self[pager.offset, pager.per_page].to_a
end
end
end
require'will_paginate/collection'
类数组
#对静态数组进行分页(提取其子集)。结果是一场灾难
#WillPaginate::Collection实例,它是一个包含更多
#有关其分页状态的属性。
#
#参数:
#*:页面-当前页面,默认为1
#*:每页-每页的项目限制,默认为30
#*:total_entries-数组中的项目总数,默认为
#array.length(显然)
#
#例如:
#arr=['a','b','c','d','e']
#paged=arr.paginate(:per_page=>2)#->['a','b']
#paged.total#u条目#->5
#arr.paginate(:page=>2,:per_page=>2)#->['c','d']
#arr.paginate(:page=>3,:per_page=>2)#->['e']
#
#这种方法最初是由Desi提出的
#McAdam}[http://www.desimcadam.com/archives/8]后来证明是
#最有用的方法是对库进行分页。
def分页(选项={})
页面=选项[:页面]| | 1
每页=选项[:每页]| | WillPaginate.per|page
total=选项[:total_entries]| | self.length
WillPaginate::Collection.create(页面、每页、总计)do | pager|
pager.replace self[pager.offset,pager.per\u page]。到\u a
结束
结束
结束
没用。你知道我做错了什么吗?那是因为你在查询的末尾有
。倒转。此方法将返回一个模型数组,而不是ActiveRecord::Relation
。只需将所需的排序添加到.order()
方法:
@ad_groups = AdGroup.where(is_template: false)
.paginate(:page => params[:page]).order('id DESC')
# HERE ======= |
这是因为在查询的末尾有.reverse
。此方法将返回一个模型数组,而不是ActiveRecord::Relation
。只需将所需的排序添加到.order()
方法:
@ad_groups = AdGroup.where(is_template: false)
.paginate(:page => params[:page]).order('id DESC')
# HERE ======= |