# Controllers reuse or the way to reuse code along with views

**URL:** <https://www.ruby-forum.com/t/controllers-reuse-or-the-way-to-reuse-code-along-with-views/172196>\
**Category:** Rails\
**Created:** [July 16, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/controllers-reuse-or-the-way-to-reuse-code-along-with-views/172196 "2009-07-16T15:36:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rubyfox](https://www.ruby-forum.com/u/rubyfox)\
**Post date:** [July 16, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/controllers-reuse-or-the-way-to-reuse-code-along-with-views/172196/1 "2009-07-16T15:36:47Z")

</div>

Consider the following scenario:

1. In a web application users are given an ability to customize what  
is displayed on their homepage.

2. All options they get for customization are already implemented as  
separate pages server by different controllers.

3. Now I would like to have some super-method available at fixed URL  
which will decide based on user settings which page to serve to the  
user and call the appropriate controller.

Now of course I will be told that it’s violation of main doctrines and  
basic principles 🙂 and I’m not supposed to do that, and I agree to  
some extent (seems like someone already did this anyway  
[http://www.railsonwave.com/2008/10/25/how-to-call-a-controller-s-action-from-a-different-controller/](http://www.railsonwave.com/2008/10/25/how-to-call-a-controller-s-action-from-a-different-controller/)  
).

But this in my thinking brings another question - how to reuse just  
code along with views? There is Cells plugin  
[GitHub - apotonick/cells\_examples: A rails project to demonstrate cells in action.](http://github.com/apotonick/cells_examples/tree/master) but I’m sort of  
wary about bringing this in, especially since ActionControllers  
already have what I need, i.e. the code with attached views.

Moving the shared code to the module will work but only to the some  
extent since there will be no conventional place for templates, no  
simple way to define filters, etc.

Probably there is something I’m missing out?

TIA,  
Evgeniy

---

<div class="post-metadata">

**Author:** ![rubyfox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4bbf92/32.png) [@rubyfox](https://www.ruby-forum.com/u/rubyfox)\
**Post date:** [July 17, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/controllers-reuse-or-the-way-to-reuse-code-along-with-views/172196/2 "2009-07-17T20:25:16Z")

</div>

Evgeniy D. wrote:

> 1. Now I would like to have some super-method available at fixed URL  
> which will decide based on user settings which page to serve to the  
> user and call the appropriate controller.

Personally, I would just write a controller that, after choosing the  
proper controller, just redirects to the URL for that controller. Any  
reason not to do that?

> But this in my thinking brings another question - how to reuse just  
> code along with views? There is Cells plugin  
> [GitHub - apotonick/cells\_examples: A rails project to demonstrate cells in action.](http://github.com/apotonick/cells_examples/tree/master) but I’m sort of  
> wary about bringing this in, especially since ActionControllers  
> already have what I need, i.e. the code with attached views.
> 
> Moving the shared code to the module will work but only to the some  
> extent since there will be no conventional place for templates, no  
> simple way to define filters, etc.

I’m not quite sure what you mean? There’s no official built-in way to  
re-use code amongst controllers. But you can use any OO technique you  
want. Sure, you can put it in a module. Since you’ll be including the  
module in existing controllers, conventional place for templates and  
filters etc will remain pretty much the same. Code in a module ends up  
getting executed ‘as if’ it was in the class that included it, right?  
It’ll still look for templates in the same places as usual. If you have  
views shared between different controllers, just make a ‘shared’ sub-dir  
in your views dir, and refer to the template specifically as  
‘shared/template\_name’ instead of just ‘template\_name’. Or you can call  
it anything you want, not just ‘shared’. This technique works for  
sharing view templates between controllers regardless of whether you  
need to share logic between controllers or not.

I think I’d use a module for shared code between controllers. You could  
also use a common superclass for multiple controllers, but I don’t think  
I’d do that. You could also put common code in an entirely seperate  
library class, that the controller gets a reference to:  
@more\_controller\_logic = MyClass.new ;  
@more\_controller\_logic.do\_something. Depending on what it is you want  
to do, that may or may not be convenient.

I think a module is probably what you want. If there’s a specific kind  
of logic you want to put in your module shared between controllers that  
for some reason doesn’t seem to be working… try asking with that  
specific example. I think a module should work fine.

Jonathan
