Formatting Data in rhtml

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

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)

in .rhtml file:

<% display_currency(@price) %>


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.

<%= number_to_currency(@price) %>

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

View the Helpers of ActiveView to find more.

Bob S.

Thank you!!

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


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.


Brandon P. wrote:

other posts, and I tried including it in my application controller. It

Thank you,

Brandon P.

Eric G.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs