# Contextually aware controllers

**URL:** <https://www.ruby-forum.com/t/contextually-aware-controllers/161519>\
**Category:** Rails\
**Created:** [February 27, 2009, 2:59am UTC](https://www.ruby-forum.com/t/contextually-aware-controllers/161519 "2009-02-27T02:59:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@spike](https://www.ruby-forum.com/u/spike)\
**Post date:** [February 27, 2009, 2:59am UTC](https://www.ruby-forum.com/t/contextually-aware-controllers/161519/1 "2009-02-27T02:59:57Z")

</div>

Hi everyone, my CMS app is starting to get quite large. I have heaps  
of modules (about 15) and some of these modules are re-used within  
different contexts:

An example is that you can add a Page to the schools website, but you  
can also add a page to a ‘Learning Cave’ which is our online learning  
area for kids. While all the controller and view code is essentially  
the same, we have it spread out (or copied) into two seperate  
controllers because the layout for a Learning Cave is different to the  
layout in the admin area where teachers add pages. Anyway, what I’m  
wanting to be able to do is know the context in which my controller is  
being called and use this to decide whether to run certain filters,  
and also to decide the layout to use and other small things. This way  
I can combine all the controller code into one controller but call it  
from multiple places and have it respond differently depending on the  
context. Here’s an example:

/admin/pages/new - shows the new page form using the layout for the  
admin area (might also do some permission checking on the side that’s  
only relevant to the admin area)

/learning\_cave/pages/new - shows the same view and uses the same  
controller but uses a different layout and possibly some other checks  
that are only relevant for caves.

I’ve been attempting to do this with restful routing and  
polymorphic\_path (because I won’t know the context ahead of time) but  
I’m having no luck. Does anyone know if there is already anything out  
there that would assist with this approach? I’m trying to package all  
my modules as rails engines and something like this would help  
greatly.

Cheers,

Brendon
