# Defining custom functions to use in views

**URL:** <https://www.ruby-forum.com/t/defining-custom-functions-to-use-in-views/89255>\
**Category:** Rails\
**Created:** [March 19, 2007, 12:56am UTC](https://www.ruby-forum.com/t/defining-custom-functions-to-use-in-views/89255 "2007-03-19T00:56:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![and80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@and80](https://www.ruby-forum.com/u/and80)\
**Post date:** [March 19, 2007, 12:56am UTC](https://www.ruby-forum.com/t/defining-custom-functions-to-use-in-views/89255/1 "2007-03-19T00:56:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![and80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@and80](https://www.ruby-forum.com/u/and80)\
**Post date:** [March 19, 2007, 1:14am UTC](https://www.ruby-forum.com/t/defining-custom-functions-to-use-in-views/89255/2 "2007-03-19T01:14:36Z")

</div>

Hemm… I just discovered the existence of the app/helpers folder  
and it seems it does what I was looking for…

😉

---

<div class="post-metadata">

**Author:** ![and80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@and80](https://www.ruby-forum.com/u/and80)\
**Post date:** [March 19, 2007, 3:29am UTC](https://www.ruby-forum.com/t/defining-custom-functions-to-use-in-views/89255/3 "2007-03-19T03:29:45Z")

</div>

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.
