How to set different page size for the first page in Kaminari?

后端 未结 2 1836
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 16:00

I have a number of objects I would like to paginate using Kaminari. However, on the first page I would also like to show a notification allowing the viewer to create his own

2条回答
  •  -上瘾入骨i
    2021-01-13 16:32

    After some more digging on the internet, I found an interesting segment in 'Kaminari recipes' about paginating arrays, that used Ruby's instance_eval method to manually paginate an array.

    I tried using this instance_eval myself, and it seems that this seems to work, although it looks rather hacky

    @page = (params[:page] || '1').to_i
    
    if @page == 1
      @alphabet = Alphabet.recent.limit(4)
    else
      @alphabet = Alphabet.recent.limit(6).offset(@page*6-8)
    end
    
    @alphabet.instance_eval <<-EVAL
      def current_page
        #{@page}
      end
      def total_pages
        ((Alphabet.all.count+2)/6.0).ceil
      end
    EVAL
    

    I'm sure there is some better way out there, but since this seems to do the trick for now, I will leave it as is.

提交回复
热议问题