# Formatting input/output of a text box

**URL:** <https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018>\
**Category:** Rails\
**Created:** [January 23, 2006, 12:50am UTC](https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018 "2006-01-23T00:50:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 23, 2006, 12:50am UTC](https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018/1 "2006-01-23T00:50:15Z")

</div>

Hello,

What’s the best way to handle formating of a text\_area? I have a  
text\_area  
in my rails app where people can leave comments and I’d like to  
automatically convert the line breaks into   
 tags, or just simply  
wrap  
a whole paragraph in

 tags. I’m currently doing the following in  
my  
comments controller to insert line breaks when the user presses   
within the text\_area, however I know there has to be a better way.

comment\_body = @comment.body  
comment\_body.gsub!( /(.\*)(\n)/, ‘\1  
’ )  
@comment.body = comment\_body

Thanks!  
Dave H.

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 23, 2006, 12:59am UTC](https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018/2 "2006-01-23T00:59:22Z")

</div>

Dave,

This one works for output and goes straight in the view:

\<%= todo.description.gsub “\n”,’  
’ %\>

With this in a list action:

def list\_todos  
@todos

```
# lot of parms not necesarry ofcourse
@todo_pages, @todos = paginate(:todos, options={:per_page => 10, :order 

```

=\>  
‘prio DESC’})  
end

Regards,

Gerard.

On Monday 23 January 2006 00:49, Dave H. tried to type something  
like:

> comment\_body.gsub!( /(.\*)(\n)/, ‘\1  
> ’ )  
> @comment.body = comment\_body
> 
> Thanks!  
> Dave H.

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 23, 2006, 1:50am UTC](https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018/3 "2006-01-23T01:50:34Z")

</div>

I think you would be better off to leave the raw data (maybe you want to  
email the comment some day??) alone and only translate the line feeds to  
br’s in your views when presenting the data. Use Gerards response to do  
that.

Bob S.

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [January 23, 2006, 1:56am UTC](https://www.ruby-forum.com/t/formatting-input-output-of-a-text-box/53018/4 "2006-01-23T01:56:37Z")

</div>

That’s a good idea. I never thought about that. I think I will leave it  
in  
it’s raw form and use Gerard’s way.

Thanks Bob,  
Dave
