So I've got pagination with Kaminiari set up on my app, which is a forum. When someone replies to the thread, I want to direct them to the last page of the thread. It seems like it'd be easy enough to hard code the logic to get the last page based on what I'm using for record numbers, but is there a built in method to get the last page?
In my current version of kaminari (v0.12.4) the following works:
users = User.page(params[:page]) last_page = users.num_pages
num_pages is defined in https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/page_scope_methods.rb.
If you want to add a
last_page method, I suggest the following:
module Kaminari module PageScopeMethods def last_page num_pages end end end
It seems that this thread has an answer.
You can write a method to achieve this if not already present in Kaminari . This should be present since Kaminari also renders the page numbers for navigation.
Say, @records is list of db records where you performed @records.page(1) to show the get the current set of records,
The last page number is defined by
(@records.total_count / per_page.to_f).ceil .
For other people's sake, I'll share what worked for me.
In Conversation show page, I want to paginate every 10 messages
messages = conversation.messages.page(params[:page]).per(10) last_page = messages.total_pages
Then I want to create a link to this show page which will show me the last page. I just made a helper method
def create_link_to_last_page(conversation) content_tag :div do link_to("Show", url_for(controller: 'conversations', action: 'show', id: conversation.id, page: last_page)) end end