kaminari - redirect to last page

Go To StackoverFlow.com


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?

2012-04-04 02:43
by DVG
What version of Kaminari are you using - Kostas 2012-05-08 13:51


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
2012-05-08 13:55
by Kostas


It seems that this thread has an answer.

2012-04-04 02:47
by Elena G. Washington
Yeah, I gave that a shot, but it says num_pages isn't defined - DVG 2012-04-04 12:13


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 .

2012-04-04 18:01
by Sairam


For other people's sake, I'll share what worked for me.


  1. Conversation has_many Messages
  2. Message belongs_to Conversation

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))
2018-05-23 02:04
by reiallenramos