[Rails] Re: using a paginate method to display the second group of 25 records on page 2
John Merlino wrote in post #989291:
>
> ((@all_resources.size - 1) / @limit).to_i + 1
>
> What does the size method do and why add one at the end?
>
Suppose 100 records, with a page size of 30
num_pages = ((100-1)/30)
num_pages = 99/30
num_pages = 3.3
num_pages = 3 (to_i)
num_pages = 4
3 full pages of 30 records, and 1 page with 9 records
Suppose 90 records...
((90-1)/30).to_i + 1 = 3, 3 full pages
It's just some math hijinks to make sure any partial page isn't missed
in the page count.
The slice(offset,limit) is the elegant part. Start at the "offset", and
return "limit" items.
On page 2, the offset is 1*25, skipping the records that would be
returned on page 1, and returns the next page's records.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home