Forum: Ruby on Rails Pagination: Last Page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John D. (Guest)
on 2009-06-08 15:43
Hi Folks

is there any way to link to the last page of a paginated list, if the
number of pages is not available in this moment? For example a forum
with topic and replies. I'd like to link to the last page of the
replies, but I don't know how many pages there are in the forum view?
I'm using mislav-will_paginate for pagination.

Thank you in advance!
Max W. (Guest)
on 2009-06-08 18:06
Patrick Heneise wrote:
> Hi Folks
>
> is there any way to link to the last page of a paginated list, if the
> number of pages is not available in this moment? For example a forum
> with topic and replies. I'd like to link to the last page of the
> replies, but I don't know how many pages there are in the forum view?
> I'm using mislav-will_paginate for pagination.
>
> Thank you in advance!

If you have an object that you have paginated, you can get the number of
pages with .total_pages: this will be the same as the number of the last
page.

Presumably when you're looking at the topic, you've already loaded it's
replies anyway?

eg

@topic = Topic.find(params[:id])
@replies = @topic.replies.paginate(:page => 1, :per_page => 20)

last_page = @replies.total_pages
John D. (Guest)
on 2009-06-08 21:31
Max W. wrote:
> If you have an object that you have paginated, you can get the number of
> pages with .total_pages: this will be the same as the number of the last
> page.
>
> Presumably when you're looking at the topic, you've already loaded it's
> replies anyway?
>
> eg
>
> @topic = Topic.find(params[:id])
> @replies = @topic.replies.paginate(:page => 1, :per_page => 20)
>
> last_page = @replies.total_pages

Well, the problem is that the replies are not yet paginated. It's a
summary on a landing-page and the last three replies should show up for
each topic. The total_pages attribute is not know at this point.
Py J. (Guest)
on 2009-06-09 04:21
i had the same problem. some people already asked this feature to
be added to will_paginate, apparently the developer doesn't think
it is a good idea. check this:

http://groups.google.com/group/will_paginate/brows...

for my projects i made an extension to make will_paginate accept
:page => :last.

Patrick Heneise wrote:
> Max W. wrote:
>> If you have an object that you have paginated, you can get the number of
>> pages with .total_pages: this will be the same as the number of the last
>> page.
>>
>> Presumably when you're looking at the topic, you've already loaded it's
>> replies anyway?
>>
>> eg
>>
>> @topic = Topic.find(params[:id])
>> @replies = @topic.replies.paginate(:page => 1, :per_page => 20)
>>
>> last_page = @replies.total_pages
>
> Well, the problem is that the replies are not yet paginated. It's a
> summary on a landing-page and the last three replies should show up for
> each topic. The total_pages attribute is not know at this point.
John D. (Guest)
on 2009-06-09 10:58
PP Junty wrote:
> i had the same problem. some people already asked this feature to
> be added to will_paginate, apparently the developer doesn't think
> it is a good idea. check this:
>
> 
http://groups.google.com/group/will_paginate/brows...
>
> for my projects i made an extension to make will_paginate accept
> :page => :last.

Thanks for this hint. Too bad Mislav doesn't accept this point. Do you
have a link for your extension?

Thank you!
Py J. (Guest)
on 2009-06-09 13:20
Attachment: will_paginate_last.zip (0 Bytes)
ok, i made a little plugin with this functionality, it is attached.
definitely not a pro solution but may be useful. could you test
it and see if it is working? if it is i could publish it on github.
this plugin also alter another behavior that i find annoying, which is:
when you request a page greater than the total pages will_paginate
returns an empty page. that happens specially when the last item of
a page is deleted. the extension will always return the last page in
this case.

Patrick Heneise wrote:
> Thanks for this hint. Too bad Mislav doesn't accept this point. Do you
> have a link for your extension?
>
> Thank you!
Py J. (Guest)
on 2009-06-09 13:26
just a correction.
by
"that happens specially when the last item of a page is deleted."
i meant
"that happens specially when the last page has only one item and the
item is deleted."
John D. (Guest)
on 2009-06-11 12:21
PP Junty wrote:
> ok, i made a little plugin with this functionality, it is attached.
> definitely not a pro solution but may be useful. could you test
> it and see if it is working? if it is i could publish it on github.
> this plugin also alter another behavior that i find annoying, which is:
> when you request a page greater than the total pages will_paginate
> returns an empty page. that happens specially when the last item of
> a page is deleted. the extension will always return the last page in
> this case.

Thanks for the code. Didn't catch the attachment at first sight. I
tested the plugin and it works quite fine. However, I have a small
problem while selecting a specific page. :page => :last always redirects
to the last page, even if a parameter is given. I tried to check the
parameter before, but this throws an error as well:

if (params[:page].nil?)
  @replies = @bullet.replies.paginate :page => :last, :order =>
'created_at ASC', :per_page => 10
else
  @replies = @bullet.replies.paginate :page => params[:page], :order =>
'created_at ASC', :per_page => 10
end

Throws:
comparison of String with 2 failed

/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
`>'
/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
`paginate'
Py J. (Guest)
on 2009-06-11 14:43
you should use blank? instead of nil? because even the original
will_paginate will throw an error if you pass an empty string.
and you should not rely on what you receive in the param :page as
a user could manually alter the url to some strange value. what
i do is to have a parse method that ensure that will_paginate is
getting a valid page number, that is why i didn't bother throwing
a more informative error message, i'll see if it can be improved.

> Thanks for the code. Didn't catch the attachment at first sight. I
> tested the plugin and it works quite fine. However, I have a small
> problem while selecting a specific page. :page => :last always redirects
> to the last page, even if a parameter is given. I tried to check the
> parameter before, but this throws an error as well:
>
> if (params[:page].nil?)
>   @replies = @bullet.replies.paginate :page => :last, :order =>
> 'created_at ASC', :per_page => 10
> else
>   @replies = @bullet.replies.paginate :page => params[:page], :order =>
> 'created_at ASC', :per_page => 10
> end
>
> Throws:
> comparison of String with 2 failed
>
> 
/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
> `>'
> 
/Users/patrick/Sites/insight/vendor/plugins/will_paginate_last/lib/will_paginate_last/finder.rb:39:in
> `paginate'
John D. (Guest)
on 2009-06-12 13:05
PP Junty wrote:
> you should use blank? instead of nil? because even the original
> will_paginate will throw an error if you pass an empty string.
> and you should not rely on what you receive in the param :page as
> a user could manually alter the url to some strange value. what
> i do is to have a parse method that ensure that will_paginate is
> getting a valid page number, that is why i didn't bother throwing
> a more informative error message, i'll see if it can be improved.

I solved the problem with a simple integer validation:

@page = params[:page].to_i
if (@page != 0)
  @replies = @bullet.replies.paginate :page => @page, :order =>
'created_at ASC', :per_page => 10
else
  @replies = @bullet.replies.paginate :page => :last, :order =>
'created_at ASC', :per_page => 10
end

Works pretty good.

Thank you for this enhancement to will_paginate. You should definitely
put it to github.
This topic is locked and can not be replied to.