# Pagination

**URL:** <https://www.ruby-forum.com/t/pagination/102911>\
**Category:** Rails\
**Created:** [March 29, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/pagination/102911 "2006-03-29T23:36:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 29, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/pagination/102911/1 "2006-03-29T23:36:54Z")

</div>

Hey,

I have a CMS I am building right now and I would like to my articles to  
split in many pages.

Here is what I want to do:

Once the article reaches 150 words or so it create a link to Page 2 of  
the article and, on page 2, it displays the rest of the article…

How can I do that ?  
(note that you explain this to a beginner 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 30, 2006, 2:01am UTC](https://www.ruby-forum.com/t/pagination/102911/2 "2006-03-30T02:01:42Z")

</div>

Weirdmonkey,

I will not speak for everyone, but I’d rather see your answer at  
least addressed than go unanswered. Your question is very vague and  
could be implemented more ways than members on this list.

You admit that you are a beginner, so I would study up a bit on your  
string routines and begin to design and implement this system with  
your own ideas, then when you come up against a specific problem, ask  
the list. It will be much more responsive. This is also the only way  
you will really learn.

I am not speaking out of criticism, but just trying to help you use  
the list to it’s best potential and also further your development  
skills. It seems like your question is almost, how do I build a CMS  
system? Maybe I have missed the main intent of the question, and if  
so, maybe if you restate it others will understand it better as well  
as myself.

Jeremy

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 30, 2006, 2:46am UTC](https://www.ruby-forum.com/t/pagination/102911/3 "2006-03-30T02:46:23Z")

</div>

Jeremy,

I understand that my question my be a little vague.

Here are more details:

It is a website for books summary

I build a scaffold to be able to enter new summaries with ease.

I call the summary in the view using \<%= book.summary %\> and would like  
to implement the truncate method to it.  
([Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000502))

What I would like to do is:

-If the # of the number of words exceed 200 for example, it will create:  
Part1 —\> word 1 to word 200  
Part2 —\> word 201 to 400  
Part3 —\> word 401 to …  
etc…

If the number of words in Part 1 exceeds 200 words, it will place a link  
to Part 2 at the bottom and so on…

I hope it clearer for you now 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 30, 2006, 3:20am UTC](https://www.ruby-forum.com/t/pagination/102911/4 "2006-03-30T03:20:13Z")

</div>

I think you’ll want to implement this back in your model. So rather  
than  
using \<%= book.summary =\> in your view you’ll use \<%=  
book.summary\_page(1)  
=\> \<%= book.summary\_page(2) =\> etc…

book.summary\_page(n) will get the appropriate chunk of text for the  
current  
page. Try something like that and see how you travel.

cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 30, 2006, 10:27am UTC](https://www.ruby-forum.com/t/pagination/102911/5 "2006-03-30T10:27:47Z")

</div>

Ben and Kaz A. wrote:

> I think you’ll want to implement this back in your model. So rather  
> than using \<%= book.summary =\> in your view you’ll use \<%=  
> book.summary\_page(1) =\> \<%= book.summary\_page(2) =\> etc…
> 
> book.summary\_page (n) will get the appropriate chunk of text for the  
> current page. Try something like that and see how you travel.

I tried something a little like that a couple of projects ago. Where I  
ended up having problems was how to deal with HTML tags in the text. I  
ended up with a separate Page model, and just let the users do the  
splitting in the end. Much simpler 🙂

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 30, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/pagination/102911/6 "2006-03-30T23:58:44Z")

</div>

Alex Y. wrote:

> Ben and Kaz A. wrote:
> 
> > I think you’ll want to implement this back in your model. So rather  
> > than using \<%= book.summary =\> in your view you’ll use \<%=  
> > book.summary\_page(1) =\> \<%= book.summary\_page(2) =\> etc…
> > 
> > book.summary\_page (n) will get the appropriate chunk of text for the  
> > current page. Try something like that and see how you travel.
> 
> I tried something a little like that a couple of projects ago. Where I  
> ended up having problems was how to deal with HTML tags in the text. I  
> ended up with a separate Page model, and just let the users do the  
> splitting in the end. Much simpler 🙂

Hi Alex,

I don’t get it, can you explain further ?

---

<div class="post-metadata">

**Author:** ![weirdmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b782af/32.png) [@weirdmonkey](https://www.ruby-forum.com/u/weirdmonkey)\
**Post date:** [March 31, 2006, 10:11am UTC](https://www.ruby-forum.com/t/pagination/102911/7 "2006-03-31T10:11:20Z")

</div>

weirdmonkey wrote:

> > ended up having problems was how to deal with HTML tags in the text. I  
> > ended up with a separate Page model, and just let the users do the  
> > splitting in the end. Much simpler 🙂
> 
> Hi Alex,
> 
> I don’t get it, can you explain further ?

Sure.

Because splitting a long body of text reliably and well is hard to get  
right (you don’t want to split in the middle of a sentence, you don’t  
want to leave dangling sentences, you can’t break markup, images might  
want to stay on the same page as a specific piece of text, and so on),  
don’t store the text in a single field in the Book model. Rather, do  
something like this:

create table books (  
id int not null auto\_increment primary key,  
title text  
– whatever else you need here…  
);  
create table pages (  
id…  
book\_id int,  
position int,  
content text  
– whatever else you need here…  
);

Then your models could look like this:

class Book \< ActiveRecord::Base  
has\_many :pages, :order =\> “position”, :dependent =\> :delete  
end  
class Page \< ActiveRecord::Base  
belongs\_to :book  
acts\_as\_list :scope =\> :book  
end

Now, in your interface, you can allow the users to create pages as they  
need them, putting the breaks where it makes sense in the text. You can  
still validate new pages as being shorter than a given limit if you want  
to, but there’s no need to do any more than that. I’ve found that the  
users I deal with are fairly happy to do things that way.

Hope this helps…
