# How to avoid repeating code in controller

**URL:** <https://www.ruby-forum.com/t/how-to-avoid-repeating-code-in-controller/99676>\
**Category:** Rails\
**Created:** [July 24, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-code-in-controller/99676 "2006-07-24T21:59:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 24, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-code-in-controller/99676/1 "2006-07-24T21:59:50Z")

</div>

Hi,  
I created a calendar class and i now have a method which gives me a 1  
day view with timeslots etc. now i want to re-use that code for  
generating 2 and 5 day views. how can i re-use my code so i can make a  
show\_two\_days class which uses the logic of the show\_one\_day class?

am i correct it can’t be done with helpers?

regards,

Remco

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 24, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-code-in-controller/99676/2 "2006-07-24T22:01:30Z")

</div>

Remco Hh wrote:

> Hi,  
> I created a calendar class and i now have a method which gives me a 1  
> day view with timeslots etc. now i want to re-use that code for  
> generating 2 and 5 day views. how can i re-use my code so i can make a  
> show\_two\_days class which uses the logic of the show\_one\_day class?
> 
> am i correct it can’t be done with helpers?
> 
> regards,
> 
> Remco

I dunno if I understand you but you might have a look to  
render\_component

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 24, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-code-in-controller/99676/3 "2006-07-24T22:34:30Z")

</div>

If they’re in the same controller, one method can call another. If  
not, put the shared method in application.rb and it becomes available  
from every controller.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000
