Reputation: 351
I'm using kaminari gem for pagination. The problem is that I need to paginate on the first page after 4 elements, and on the all other pages after 25 elements. It it possible to configure kaminari to solve my problem? Here is a usage:
.pagination
.pagination__back
- if params[:page] && params[:page].to_i > 1
= link_to "Previous news", news_items_path(page: params[:page].to_i - 1)
- else
= ""
.pagination__forward
- if params[:page]
= link_to "Next news", news_items_path(page: params[:page].to_i + 1)
- else
= link_to "Next news", news_items_path(page: 2)
Upvotes: 0
Views: 337
Reputation: 5482
First of all you can leave out all the code related to forward/previous pages. Kaminari solves this with its helper already. For that matter, inside your view, use the following code:
= paginate @your_resource
This will render several ?page=N pagination links surrounded by an HTML5 tag. (Source)
To have a paginated resource, you want to add the following code to your controller:
@your_resource = YourResource.order(:foobar).page(params[:page])
# params[:page] will get added to each "paginated" request by Kaminari
# if you use its previously mentioned helper method.
Now you want a dynamic limit. For that matter I suggest adding something like this:
def index
@your_resource = YourResource.order(:foobar).page(params[:page]).per(dynamic_limit(params[:page]))
end
private
def dynamic_limit(current_page = 1)
if current_page == 1
return 4
else
return 25
end
end
This way you will check for the current page, and if it's the first page it'll limit the results to 4. Otherwise, 25.
Upvotes: 1