# Controllers More Than One Sub-Directory Deep?

**URL:** <https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538>\
**Category:** Rails\
**Created:** [March 8, 2006, 12:53am UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538 "2006-03-08T00:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeLynn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@DeLynn\_B](https://www.ruby-forum.com/u/DeLynn_B)\
**Post date:** [March 8, 2006, 12:53am UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538/1 "2006-03-08T00:53:45Z")

</div>

Does anyone know how to make Rails aware of controllers that might  
exist in directories that exist deeper than one level under the  
app/controllers directory? I initially figured just adding additional  
scope information might do the trik (i.e. Tabs::Admin::OneController  
or Admin::Tabs::OneController), but that does not seem to work.

I’m guessing that I can just manually add these directories to the  
environment load path, but I just wanted to make sure there wasn’t a  
more elegant way of getting this to work. Thanks.

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com/)**

---

<div class="post-metadata">

**Author:** ![DeLynn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@DeLynn\_B](https://www.ruby-forum.com/u/DeLynn_B)\
**Post date:** [March 8, 2006, 4:41am UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538/2 "2006-03-08T04:41:56Z")

</div>

can’t you just create a controller like /script/generate controller  
somedir/someotherder/somecontroller

---

<div class="post-metadata">

**Author:** ![DeLynn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@DeLynn\_B](https://www.ruby-forum.com/u/DeLynn_B)\
**Post date:** [March 8, 2006, 5:01am UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538/3 "2006-03-08T05:01:50Z")

</div>

Unless you have a really large, complex app, you might not want to do  
this. It’s obviously tempting to put admin controllers in a  
controllers/admin directory, the corresponding views in  
views/admin/controllername and so on, but you may later find that an  
engine or plugin you want to use won’t play well with this.

I tried it about a month ago not because my codebase was espcially big,  
but because I was going to have authentication handled by the HTTP  
server instead of by Rails and that’s the sensible way to organize code  
around auth realms when you’re doing it with the webserver config. But  
then I went with a much easier and powerful auth scheme using one of the  
half dozen or so available plugins to handle authentication and  
attribute-level model security in Rails instead. It crapped out on the  
subdirectories and namespacing. It took me twenty minutes to scrap the  
subdirectories, edit my models and controllers to flatten the namespace  
and I haven’t looked back. I’m sure there are quite a few other plugins  
and other useful components out there that don’t play well with  
namespaced and subdirectoried view and controller directories.

DeLynn B. wrote:

> Does anyone know how to make Rails aware of controllers that might  
> exist in directories that exist deeper than one level under the  
> app/controllers directory? I initially figured just adding additional  
> scope information might do the trik (i.e. Tabs::Admin::OneController  
> or Admin::Tabs::OneController), but that does not seem to work.
> 
> I’m guessing that I can just manually add these directories to the  
> environment load path, but I just wanted to make sure there wasn’t a  
> more elegant way of getting this to work. Thanks.
> 
> –  
> DeLynn B.  
> removed\_email\_address@domain.invalid  
> [http://www.delynnberry.com](http://www.delynnberry.com)

---

<div class="post-metadata">

**Author:** ![DeLynn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@DeLynn\_B](https://www.ruby-forum.com/u/DeLynn_B)\
**Post date:** [March 8, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538/4 "2006-03-08T17:58:01Z")

</div>

> can’t you just create a controller like /script/generate controller  
> somedir/someotherder/somecontroller

You know what, I thought the same thing (and even tried it). Then  
after seeing your email, I decided to try it again with a new Rails  
application, and everything worked as expected! This confused me quite  
a bit, and figured I had just done something incorrect in my original  
application. So, I copied the controllers from the new application to  
my existing application, but it still didn’t work.

Confused as to why this might be happening, I started duplicating as  
much of my existing application to this newly created application  
trying to find the point where things broke. What I discovered was  
that everything worked right up to the point where I drop in Edge  
Rails to get the 1.1 goodness that’s included there.

So now my question is, what has changed in 1.1 that would cause these  
problems? I have detailed my specific application layout below.

app/controllers  
base\_controller.rb (Base \< AC)  
/admin  
base\_controller.rb (Admin::Base \< Base)  
location\_controller.rb (Admin::LocationController \<  
Admin::BaseController)  
/tabs  
details\_controller.rb (Admin::Tabs::DetailsController \<  
Admin::LocationController

The error I get when trying to navigate to /admin/tabs/details is  
“uninitialized constant Admin::LocationController”. Here is the stack  
trace that is generated:

#{RAILS\_ROOT}/vendor/rails/activesupport/lib/active\_support/dependencies.rb:106:in  
`const\_missing’

#{RAILS\_ROOT}/vendor/rails/actionpack/lib/action\_controller/routing.rb:236:in  
`traverse\_to\_controller’

generated/routing/recognition.rb:46:in `eval’

generated/routing/recognition.rb:46:in `recognize\_path’

I’m going to start digging around the 1.1 source and see what has  
changed since 1.0 in the files listed above. Thanks.

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com/)**

---

<div class="post-metadata">

**Author:** ![DeLynn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@DeLynn\_B](https://www.ruby-forum.com/u/DeLynn_B)\
**Post date:** [April 1, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/controllers-more-than-one-sub-directory-deep/54538/5 "2006-04-01T16:22:58Z")

</div>

Hi,  
I think I ran into the same problem. I have my models in subdirectories  
and as long as i reference models within the same subdirectory,  
everything seems to work. As soon as I traverse directories, it breaks  
with the “uninitialized constant” error (also see my post from today  
regarding this).

Regards  
Juergen
