Wilson,
I feel I’m very close, but I keep getting a funny error : (full
stacktrace at bottom)
undefined method `zero?' for "2":String
, where ‘2’ is my_collection.size
I looked into Rails’ source, and it happens in the Paginator code :
def page_count
@page_count ||= @item_count.zero? ? 1 : ## <<---- ERROR HERE
(q,r=@item_count.divmod(@items_per_page);
r==0? q : q+1)
end
‘@item_count’ seems to be a string. How is this possible: the only place
its value is changed is in the constructor:
class Paginator
...
def initialize(controller, item_count, items_per_page,
current_page=1)
…
@item_count = item_count || 0
end
And ‘item_count’, the 2nd parameter, is set to the collection.size
pages = Paginator.new self, collection.size,
options[:per_page], options[:page]
Alain
Full code :
class Member_controller < …
def list
@project = Project.find(1)
@members, @member_pages = paginate_collection @project.members ,
:page => params[:page]
end
…
end
class ApplicationController < ActionController::Base
def paginate_collection(collection, options = {})
# Invoke it from a controller like this:
# @pages, @users = paginate_collection(@some_collection, :page =>
params[:page])
default_options = {:per_page => 10, :page => 1}
options = default_options.merge options
pages = Paginator.new self, collection.size,
options[:per_page], options[:page]
first = pages.current.offset # <<------- ERROR
OCCURS HERE
last = [first + options[:per_page],
collection.size].min
slice = collection[first…last]
return [pages, slice]
end
end
Error Stacktrace :
actionpack-1.11.2/… :
lib/action_controller/pagination.rb:253:in page_count' lib/action_controller/pagination.rb:261:in
has_page_number?’
lib/action_controller/pagination.rb:287:in initialize' lib/action_controller/pagination.rb:267:in
new’
lib/action_controller/pagination.rb:267:in []' lib/action_controller/pagination.rb:235:in
current’
#{RAILS_ROOT}/app/controllers/application.rb:11:in paginate_collection' #{RAILS_ROOT}/app/controllers/members_controller.rb:12:in
list’