# Multi line text

**URL:** <https://www.ruby-forum.com/t/multi-line-text/68606>\
**Category:** Rails\
**Created:** [September 13, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606 "2006-09-13T21:52:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/1 "2006-09-13T21:52:21Z")

</div>

Hi,

I am storing multi-lined information in a text field in the database,  
and intaking it using a text\_area field. When i output it, the  
information is all streamed into one line. How can I output the  
information with the line breaks.

Thanks!

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/2 "2006-09-13T22:06:24Z")

</div>

Replace ‘\n’ with ‘  
’ (or ‘  
\n’) in your string when rendering  
it.

Cheers.

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/3 "2006-09-13T22:08:24Z")

</div>

I define the following method in one of my helper files:

# wraps each line to width characters, splitting lines on a

whitespace character  
def wrap\_text(text, width = nil)  
char\_count = 0  
0.upto(text.length) do |i|  
point = i;  
# reset the character counter if we encounter a newline  
# before we’ve reached width characters  
char\_count = 0 if text[point…point] == “\n”;  
if char\_count \> width  
# once we hit the desired width of characters, start  
# moving backwards one character at a time until we  
# encounter a whitespace character  
point -=1 while text[point…point] != " "  
text.insert(point+1, “\n”)  
# we’ve inserted a newline, reset the character counter  
char\_count = 0;  
else  
char\_count+=1  
end  
end  
return text  
end

I’m sure there’s an easier method for line wrapping (if anyone else  
has a better method, I’d love to see it). I did come across the  
following site earlier today, which includes a _much_ simpler method  
for line wrapping, although unfortunately it doesn’t work:

[http://redhanded.hobix.com/inspect/methodCheckDateSucc.html](http://redhanded.hobix.com/inspect/methodCheckDateSucc.html)

the method is the following:

str = “Hello. Ruby rocks. " \* 50  
while space\_pos = str.rindex(” ", 80)  
str[space\_pos, 1] = “\n”  
end

but it fails to correctly line wrap, since each call to str.rindex  
doesn’t take into account where the previous newline character was  
inserted… I just didn’t want to spend any time fixing it, since I’ve  
already got a working line wrap method (albeit mine is a bit  
cumbersome).

Mike

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/4 "2006-09-13T22:09:44Z")

</div>

You might want to use the simple\_format TextHelper:

simple\_format(text)

Returns text transformed into HTML using very simple formatting rules  
Surrounds paragraphs with

tags, and converts line breaks into   
  
Two consecutive newlines(\n\n) are considered as a paragraph, one  
newline (\n) is considered a linebreak, three or more consecutive  
newlines are turned into two newlines

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/5 "2006-09-13T22:16:31Z")

</div>

Mike G. wrote:

> I’m sure there’s an easier method for line wrapping (if anyone else  
> has a better method, I’d love to see it). I did come across the  
> following site earlier today, which includes a _much_ simpler method  
> for line wrapping, although unfortunately it doesn’t work:

Mike,

There’s an existing word\_wrap TextHelper function that appears to do the  
same thing as yours. It’s implementation is also a wee bit smaller:

def word\_wrap(text, line\_width = 80)  
text.gsub(/\n/, “\n\n”).gsub(/(.{1,#{line\_width}})(\s+|$)/,  
“\1\n”).strip  
end

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 13, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/multi-line-text/68606/6 "2006-09-13T22:36:26Z")

</div>

Thanks Curtis, that’s good to know… Although I think I’ll stick to my  
current method, as this one seems to remove lines with nothing but a  
line feed on them, which I often have, since I’m displaying the  
contents of user entered text in a

```

```
 tag. With my method, I  
get pretty much exactly what the user has entered, including their  
empty linefeeds, but wrapped so that it doesn’t extend past the width  
of the page.

Mike

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [September 14, 2006, 1:20am UTC](https://www.ruby-forum.com/t/multi-line-text/68606/7 "2006-09-14T01:20:48Z")

</div>

Thanks for all the help! I ended up using the simple\_format helper as a  
base, but modified it so that it didn’t put

tags around the entire  
thing.

def simple\_format2(text)  
text.gsub!(/(\r\n|\n|\r)/, “\n”) # lets make them newlines  
crossplatform  
text.gsub!(/\n\n+/, “\n\n”) # zap dupes  
text.gsub!(/\n\n/, ‘

\0

’) # turn two newlines into paragraph  
text.gsub!(/([^\n])(\n)([^\n])/, ‘\1\2  
\3’) # turn single  
newline into   
  
end

Thanks again!
