Rails paginate for collections
To generate pagination for your collections, add the following action to your controller.
def paginate_collection(collection, options = {})
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
last = [first + options[:per_page], collection.size].min
slice = collection[first...last]
return [pages, slice]
end
Now you can call the custom paginate_collection in your controller as follows.
@female_students = Student.find(:all).select do |student|
student.sex == 'female'
end
@student_pages, @students = paginate_collection @female_students, :per_page => 10
About this entry
You’re currently reading “Rails paginate for collections,” an entry on Thoughts Unlimited
- Published:
- October 19, 2006 / 4:48 pm
- Category:
- Ruby
- Tags:
No comments yet
Jump to comment form | comments rss [?] | trackback uri [?]