# Limiting Size of Output Field

**URL:** <https://www.ruby-forum.com/t/limiting-size-of-output-field/118353>\
**Category:** Rails\
**Created:** [October 25, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/limiting-size-of-output-field/118353 "2007-10-25T23:40:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 25, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/limiting-size-of-output-field/118353/1 "2007-10-25T23:40:28Z")

</div>

Hi!

I have a really stupid/simple question. How do you go about limiting  
the size of a field for the output? For example, I have a field that  
says…

 \<%= response.description %\>

in my list.rhtml file. However, the description, at times, is rather  
long. How can I limit it to say, 15 or so characters and then just  
chop off the rest of the text?

Thanks!  
Mike

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 12:27am UTC](https://www.ruby-forum.com/t/limiting-size-of-output-field/118353/2 "2007-10-26T00:27:16Z")

</div>

> I have a really stupid/simple question. How do you go about limiting  
> the size of a field for the output? For example, I have a field that  
> says…
> 
> \<%= response.description %\>
> 
> in my list.rhtml file. However, the description, at times, is rather  
> long. How can I limit it to say, 15 or so characters and then just  
> chop off the rest of the text?

truncate(response.description, 15)

truncate(text, length = 30, truncate\_string = “…”)

If text is longer than length, text will be truncated to the length of  
length and the last three characters will be replaced with the  
truncate\_string.

truncate(“Once upon a time in a world far far away”, 14)  
=\> Once upon a…

Just be careful about chopping it right in the middle of an html tag…

-philip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 12:27am UTC](https://www.ruby-forum.com/t/limiting-size-of-output-field/118353/3 "2007-10-26T00:27:51Z")

</div>

\<%= response.description.to(15) %\>

-Bill

removed\_email\_address@domain.invalid wrote:

> chop off the rest of the text?
> 
> Thanks!  
> Mike
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 26, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/limiting-size-of-output-field/118353/4 "2007-10-26T15:57:28Z")

</div>

Thank you very much Bill. That .to(15) did the trick…and so  
easy. 🙂

Thanks,  
Mike
