Formatting Data in rhtml


#1

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.


#2

<%= number_to_currency(@price) %>

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

View the Helpers of ActiveView to find more.

Bob S.
http://www.railtie.net/


#3

Thank you!!

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


#4

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