Topic: Will paginate and hashes

hello, I'm using will paginate with models and it works great now I need to use it in order to paginate a session variable that contain a list of names.

is it possible to paginate an hash ???

Thanks and have a good 1 !

Re: Will paginate and hashes

Not my code.. but stick this in your env.rb file to paginate arrays..

class Array
  def paginate(all = nil, options = {})
    options[:page] = (options[:page].to_i == 0) ? 1 : options[:page].to_i
    options[:per_page] = (options[:per_page].to_i == 0) ? 30 : options[:per_page].to_i   
    pagination_array =[:page], options[:per_page], self.size)
    start_index = pagination_array.offset
    end_index = start_index + (options[:per_page] - 1)
    array_to_concat = self[start_index..end_index]
    array_to_concat.nil? ? [] : pagination_array.concat(array_to_concat)

Re: Will paginate and hashes

Thanks, but
once I have added the code on env.rb  How can I paginate arrays ???

Have a good 1 !!!

Re: Will paginate and hashes

@events = @events.paginate(:all, :page => params[:page], :per_page => params[:per_page])

Re: Will paginate and hashes

thanks buddy, I'm gonna try it

Re: Will paginate and hashes

Hello, does env.rb mean environment.rb (on configuration folder) ???

I copied your code on environment.rb and after on my view I put the code below

<% @selected_page = session[:selected].paginate(:all, :page => params[:page], :per_page => 2) 
@selected_page.each do |key,value| %>

and rebooted the server but I have this error:

undefined method `paginate' for {}:Hash

What's wrong with my code ???


Re: Will paginate and hashes

up ):

Last edited by macsig (2007-12-28 13:10:50)