Forum: Ruby on Rails Formatting Data in rhtml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed17f03ba21ba382e0229750a2b997ba?d=identicon&s=25 Brandon Pearce (brandags)
on 2006-01-27 22:27
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 Pearce
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-27 22:35
(Received via mailing list)
<%= number_to_currency(@price) %>
http://api.rubyonrails.org/classes/ActionView/Help...


<%= truncate(@description, 100) %>
http://api.rubyonrails.org/classes/ActionView/Help...

View the Helpers of ActiveView to find more.


Bob Silva
http://www.railtie.net/
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-01-27 22:38
(Received via mailing list)
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 Pearce wrote:
> other posts, and I tried including it in my application controller. It
>
>
> Thank you,
>
> Brandon Pearce
>
>
>


--
Eric Goodwin
http://www.ericgoodwin.com
Ed17f03ba21ba382e0229750a2b997ba?d=identicon&s=25 Brandon Pearce (brandags)
on 2006-01-27 22:42
Thank you!!

That is exactly what I was wanting.
So that's what helpers are for....
This topic is locked and can not be replied to.