Pagination


#1

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 :slight_smile:

Thank you!


#2

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


#3

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.
(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 :slight_smile:

Thank you!


#4

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


#5

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 :slight_smile:


#6

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 :slight_smile:

Hi Alex,

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


#7

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 :slight_smile:

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…