how to paginate with an array of file names read from a directory
I have an array of file names that are read from my public/images/pj_pics/ directory. The array has thous开发者_开发百科ands of images, and I want to have the page display a max of 100 pics per page.
I've looked at the will_paginate gem, but it seems to only be designed for data from models (https://github.com/mislav/will_paginate/wiki)
Here's how I read the file names into @f_ary:
class AvatarsController < ApplicationController
def index
@title = "pixelated avatars"
@f_ary = Dir.entries("public/images/pj_pics/")
@f_ary.delete(".")
@f_ary.delete("..")
@f_ary = @f_ary.each_slice(25).to_a
end
end
I display the images in a big table like this:
%table
%tbody
- @f_ary.each do |row|
%tr
- row.each do |column|
%td= link_to image_tag("pj_pics/#{column}", :alt => 'img')
how about something like:
in_groups_of(per_page)[page-1]
Don't need to bother with will_paginate for paginating straight forward arrays.
Also, instead of using Dir#entries, you could use a glob, that way you can specify the extensions of the files that you want to use WITHOUT removing "." and ".."
精彩评论