Forum: Ruby on Rails defining custom functions to use in views

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.
And80 (Guest)
on 2007-03-19 01:56
(Received via mailing list)
Hi all,
I am new to rails, so please forget this newbie question..
I would like to add a function for calculating the elapsed time from
the date of a post and the moment when the page is retrieved (Yes, I
am sure 1milion of people did it already..) Considering that it should
only process the data retrieved trough the model and  controller, I
would prefer to implement it as a simple utility function and not as a
method of the controller. So, the question is: is there a proper way
to add simple functions and have them accessible within the .rhtml
templates scope?

Thanks,
Andrea
And80 (Guest)
on 2007-03-19 02:14
(Received via mailing list)
Hemm... I just discovered the existence of the app/helpers folder
and it seems it does what I was looking for...

;)
Steve R. (Guest)
on 2007-03-19 04:29
(Received via mailing list)
You might look at Rails' time_ago_in_words (also known as
distance_of_time_in_words_to_now).

Assuming you have a created_at field in your Post model, you can do:

This was posted <%= time_ago_in_words(@post.created_at) %> ago.
This topic is locked and can not be replied to.