Rails Kaminari - How to order / paginate Arrays?

I follow railscast for Kaminari (http://railscasts.com/episodes/254-pagination-with-kaminari). But I am stuck with part of the controller.

In my controller, I have something like this:

def index @articles = (params[:mine] == "true") ? current_user.articles : Article.search(params[:search]) respond_to do |format| format.html format.json { render json: @articles } end end 

And now I'm not sure how to combine the methods, order, page and per, as in screencast .order("name").page(params[:page]).per(5) . I keep getting the "order" of the method in the array. I know that I can’t call methods on arrays, but how else can I bind them?

+6
source share
1 answer

You can use Kaminari in arrays:

 Kaminari.paginate_array(@articles).page(params[:page]).per(5) 

In the documentation:

Kaminari provides a wrapper class Array, which adapts the overall Array object to the view helper paginate . However, the paginate does not automatically process the Array object (this is also intentional in design). Kaminari::paginate_array converts your Array object into a paginatable array that accepts the page method.

+18
source

Source: https://habr.com/ru/post/922169/


All Articles