1
votes

I'm a beginner when it comes to ruby on rails. In my project I'm using 'kaminari' gem to paginate collection of images. I would like to reverse the original pagination so instead of:

< , 1, 2, 3, 4 ... 10, > 

*where numbers are page numbers and arrows show prev and next for the pagination view

I want to get:

<, 10, 9, 8, 7 ... 1, >

My problem is that I can't find any working solution and therefore I'm asking you guys for help. I would like to avoid using paginate_array due to optimization reason. Is there a way to simply flip the pagination from right to left? Is something like this even possible with kaminari or should I switch to something else for pagination?

1
Do you really need to flip the pagination or do you want the sorted order of images in a different order? Like newest to oldest?Anthony
you could reverse the array order i.e. <%= paginate @users.reverse %>, but m not sure about itJeet
Anthony - Unfortunately I need to flip it because of the layout that I'm suppose to create. It should have older images on the left, newer on the right. Jeet - I can't use reverse on the collection if it's not an array.Styszma
You can use sql order, limit, offset and ruby reverse in end.Зелёный
How could I implement this into code? If you could give me an example that would be amazing!Styszma

1 Answers

0
votes

You can use sql order, limit, offset and ruby reverse in end. I try implement some example but need test.

somewhere in model you have method what return page:

def users(page)
  # find right offset
  offset = if page.to_i == 1
           # if page number 1 offset be 0 
             0
           elsif page.to_i == 2
           # if page number 2 offset be 10
             10
           else
           # in another case offset must be page * 10
             page.to_i.pred * 10
           end
  # order :desc and reverse
  users.limit(10).offset(offset).order(created_at: :desc).reverse
end

limit 10 offset 0 it is like page(1).per(10) or limit 10 offset 10 it is like page(2).per(10). Try work around this.