# Formatting Data in rhtml

**URL:** <https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082>\
**Category:** Rails\
**Created:** [January 27, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082 "2006-01-27T22:27:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hawken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecccb3/32.png) [@hawken](https://www.ruby-forum.com/u/hawken)\
**Post date:** [January 27, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082/1 "2006-01-27T22:27:30Z")

</div>

Is there a simple way to format your data in the .rhtml file?  
I come from a PHP/Smarty background, and in the templates, I’ve used  
what is called “modifiers” to format data.

{$price|currency\_format} # displays in currency format  
{$description|truncate:“100”} # Truncates the description after 100  
characters

It seems like the template (rhtml) would be the ideal place to play with  
the display format. I saw a display\_currency function in one of the  
other posts, and I tried including it in my application controller. It  
runs fine if you call it from within the controller, but it won’t let me  
call it from the rhtml file.

in application controller: (or any controller)

def display\_currency(number)  
unless number.nil?  
“$” + sprintf("%01.2f",number)  
end  
end

in .rhtml file:

\<% display\_currency(@price) %\>

Error:

undefined method `display\_currency’ for #\<#Class:0x39381e8:0x3938110\>

Do I have to write my own functions for things like this or are there  
some built in? Either way, how do I use them in the .rhtml files?

Thank you,

Brandon P.

---

<div class="post-metadata">

**Author:** ![hawken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecccb3/32.png) [@hawken](https://www.ruby-forum.com/u/hawken)\
**Post date:** [January 27, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082/2 "2006-01-27T22:35:52Z")

</div>

\<%= number\_to\_currency(@price) %\>

> **[ActionView::Helpers::NumberHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html)**
>
> Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, file size, and pretty printing.

\<%= truncate(@description, 100) %\>

> **[ActionView::Helpers::TextHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)**
>
> The TextHelper module provides a set of methods for filtering, formatting and transforming strings, which can reduce the amount of inline Ruby code in your views.

View the Helpers of ActiveView to find more.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![hawken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecccb3/32.png) [@hawken](https://www.ruby-forum.com/u/hawken)\
**Post date:** [January 27, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082/3 "2006-01-27T22:42:15Z")

</div>

Thank you!!

That is exactly what I was wanting.  
So that’s what helpers are for…

---

<div class="post-metadata">

**Author:** ![hawken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecccb3/32.png) [@hawken](https://www.ruby-forum.com/u/hawken)\
**Post date:** [January 27, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/formatting-data-in-rhtml/98082/4 "2006-01-27T22:38:55Z")

</div>

Hi,

For currency you can use the number\_to\_currency() helper.  
If you want to write your own helpers you can put them in your  
application helper, if you want to use them everywhere, or any  
controller helper. All the functions that you write in these will be  
accessable from the corresponding view.

Eric

Brandon P. wrote:

> other posts, and I tried including it in my application controller. It
> 
> Thank you,
> 
> Brandon P.

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)
