# Controller Namespace Support

**URL:** <https://www.ruby-forum.com/t/controller-namespace-support/100647>\
**Category:** Engines Development\
**Created:** [February 3, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647 "2006-02-03T15:05:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/1 "2006-02-03T15:05:53Z")

</div>

Hi

I’m currently working on making ActiveRBAC (https://  
[activerbac.turingstudio.com](http://activerbac.turingstudio.com)) into an engine.

However, there seems to be a problem with Controllers and Namespaces.  
I want my controller to be in the “ActiveRBAC” namespace since  
ActiveRBAC contains controllers with more or less common names such  
as “UserController”.

A quick look at the mail archive shows me that there are known  
problems. Could someone give me an abstract of the problems? How much  
work would it be to add namespace support for controllers to rails-  
engines? I’d be willing to give it a try, then.

Regards

Manuel H.

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/2 "2006-02-03T15:18:01Z")

</div>

The problems stem from the strange way that Rails has been require-ing  
controllers within modules. Old (1.0.0, 1.0.1) versions of the Engines  
plugin would strip everything bar the filename out of the require path  
in order to quickly do the code mixing, but that also removes module  
information (since ActiveRBAC::UserController would be required as  
‘active\_rbac/user\_controller’). The biggest problem is that different  
installations of Rails seem to use different paths when require-ing  
controllers. That’s what I’ve spent the last 3 point-releases chasing.

The current version of the Engines plugin (1.0.6) _should_ fix all of  
these problems - if you find you’re having problems then post here  
(preferrable with traces from the engine logger; see Engines.log for  
details about how to turn it on) and we’ll work through it.

- james

On 2/3/06, Manuel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A quick look at the mail archive shows me that there are known  
> [http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/3 "2006-02-03T17:52:11Z")

</div>

Hi

> The current version of the Engines plugin (1.0.6) _should_ fix all of  
> these problems - if you find you’re having problems then post here  
> (preferrable with traces from the engine logger; see Engines.log for  
> details about how to turn it on) and we’ll work through it.

I did not find any documentation about how to turn on traces, the  
only thing I found was a call to Engine.create\_logger which I added  
to my environment.rb.

The source of the engine can be found here: [http://www.6URL.com/0EJE](http://www.6URL.com/0EJE)

Here is the trace:

==8\<=========================  
removed\_email\_address@domain.invalid 17:45:58 rbac\_demo $\> ./script/server  
=\> Booting WEBrick…  
Starting engine ‘active\_rbac’ from ‘/Users/manuel/Development/Rails/  
[arbac.ts.com/rbac\_demo/trunk/rbac\_demo/vendor/plugins/active\_rbac](http://arbac.ts.com/rbac_demo/trunk/rbac_demo/vendor/plugins/active_rbac)’  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/user to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/static\_permission to  
the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/shared to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/role to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/registration\_mailer to  
the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/registration to the  
load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/login to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views/group to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/views to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/model to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/helpers to the load path  
adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
rbac\_demo/vendor/plugins/active\_rbac/app/controllers to the load path  
No init\_engines.rb file found for engine ‘active\_rbac’…  
Attempting to copy public engine files from ‘./vendor/plugins/  
active\_rbac/public’  
=\> Rails application started on [http://0.0.0.0:3000](http://0.0.0.0:3000)  
=\> Ctrl-C to shutdown server; call with --help for options  
[2006-02-03 17:46:02] INFO WEBrick 1.3.1  
[2006-02-03 17:46:02] INFO ruby 1.8.2 (2004-12-25) [powerpc-  
darwin8.2.0]  
[2006-02-03 17:46:02] INFO WEBrick::HTTPServer#start: pid=6766  
port=3000  
Engines’ require\_or\_load for ‘./app/controllers/application.rb’  
finally loading from application: ‘./app/controllers/application.rb’  
Engines’ require\_or\_load for ‘application\_helper’  
requiring helper file ‘application\_helper’  
 → rewrote to ‘application\_helper’  
 → checking ‘active\_rbac’ for ./vendor/plugins/active\_rbac/app/  
helpers/application\_helper.rb  
finally loading from application: ‘application\_helper.rb’  
Engines’ require\_or\_load for ‘application\_api’  
finally loading from application: ‘application\_api.rb’  
Engines’ require\_or\_load for ‘user’  
finally loading from application: ‘user.rb’  
Engines’ require\_or\_load for ‘group’  
finally loading from application: ‘group.rb’  
Engines’ require\_or\_load for ‘role’  
finally loading from application: ‘role.rb’  
Engines’ require\_or\_load for ‘static\_permission’  
finally loading from application: ‘static\_permission.rb’  
Engines’ require\_or\_load for ‘user\_registration’  
finally loading from application: ‘user\_registration.rb’  
Engines’ require\_or\_load for ‘./vendor/plugins/active\_rbac/app/  
controllers/user\_controller.rb’  
requiring controller file ‘./vendor/plugins/active\_rbac/app/  
controllers/user\_controller.rb’  
 → rewrote to ‘user\_controller.rb’  
 → checking ‘active\_rbac’ for ./vendor/plugins/active\_rbac/app/  
controllers/user\_controller.rb  
 → found, loading from engine ‘active\_rbac’  
Engines’ require\_or\_load for ‘vendor/plugins/active\_rbac/app/helpers/  
rbac\_helper’  
requiring helper file ‘vendor/plugins/active\_rbac/app/helpers/  
rbac\_helper’  
 → rewrote to ‘rbac\_helper’  
 → checking ‘active\_rbac’ for ./vendor/plugins/active\_rbac/app/  
helpers/rbac\_helper.rb  
 → found, loading from engine ‘active\_rbac’  
finally loading from application: ‘vendor/plugins/active\_rbac/app/  
helpers/rbac\_helper.rb’  
Engines’ require\_or\_load for ‘active\_rbac’  
finally loading from application: ‘active\_rbac.rb’  
127.0.0.1 - - [03/Feb/2006:17:46:15 CET] “GET /user HTTP/1.1” 500 10329

- → /user  
==8\<=========================

Regards

Manuel H.

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/4 "2006-02-03T17:58:14Z")

</div>

“need to remember” - d’oh. Bad typing today.

On 2/3/06, James A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > these problems - if you find you’re having problems then post here
> > 
> > adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
> > rbac\_demo/vendor/plugins/active\_rbac/app/views/login to the load path  
> > No init\_engines.rb file found for engine ‘active\_rbac’…  
> > finally loading from application: ‘./app/controllers/application.rb’  
> > Engines’ require\_or\_load for ‘group’  
> > controllers/user\_controller.rb’  
> > helpers/rbac\_helper.rb
> 
> –
> 
> - J \*  
> ~

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/5 "2006-02-03T17:58:14Z")

</div>

You need to remembered to put your modularized controllers into  
subfolders, i.e. "class ActiveRbac::UserController \< … " would go  
into  
./vendor/plugins/active\_rbac/app/controllers/active\_rbac/user\_controller.rb…

- james

On 2/3/06, Manuel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rbac\_demo/vendor/plugins/active\_rbac/app/views/user to the load path  
> adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
> adding /Users/manuel/Development/Rails/arbac.ts.com/rbac\_demo/trunk/  
> darwin8.2.0]  
> Engines’ require\_or\_load for ‘application\_api’  
> finally loading from application: ‘user\_registration.rb’  
> requiring helper file 'vendor/plugins/active\_rbac/app/helpers/
> 
> - → /user  
> [http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/6 "2006-02-03T18:37:28Z")

</div>

Am 03.02.2006 um 17:55 schrieb James A.:

> You need to remembered to put your modularized controllers into  
> subfolders, i.e. "class ActiveRbac::UserController \< … " would go  
> into ./vendor/plugins/active\_rbac/app/controllers/active\_rbac/  
> user\_controller.rb…

OK.

IMO this is not exactly elegant. Shouldn’t all classes be prefixed by  
the engine’s name automatically?

Regards

Manuel H.

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 4, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/7 "2006-02-04T12:44:43Z")

</div>

Am 03.02.2006 um 18:55 schrieb James A.:

> Do you mean that every class in the engine ‘MyEngine’ should be put  
> into the module MyEngine?
> 
> i.e. class User \< ActiveRecord::Base automatically becomes class  
> MyEngine::User \< ActiveRecord::Base ?
> 
> While something like this is superficially appealing, it’s actually  
> way more complicated to implement (even Rails doesn’t support this  
> yet) and IMHO adding ‘more magic’ at this point isn’t a brilliant  
> idea.

Actually, it would be enough if I could write this in my active\_rbac  
engine:

class ActiveRbac::UserController  
# …  
end

This means that the load-routing would have to be changed…

> subdirectories on your site).  
> Do you by chance know where to look for that magic in Rails? I want  
> to have a look and could drop half an our searching if you know it  
> off your head.

Regards

Manuel H.

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 19, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/8 "2006-02-19T12:27:11Z")

</div>

James,

Would you be interested in such a change as I described in my  
previous email (quoted below).

Regards,

Manuel

Am 04.02.2006 um 12:43 schrieb Manuel H.:

> Actually, it would be enough if I could write this in my active\_rbac  
> engine:
> 
> class ActiveRbac::UserController  
> # …  
> end
> 
> This means that the load-routing would have to be changed…

snip

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 19, 2006, 1:03pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/9 "2006-02-19T13:03:26Z")

</div>

On 2/19/06, Manuel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Actually, it would be enough if I could write this in my active\_rbac  
> > engine:
> > 
> > class ActiveRbac::UserController  
> > # …  
> > end
> > 
> > This means that the load-routing would have to be changed…

At the moment, writing this code means that you would get the URL  
‘/active\_rbac/user’ - this is ‘standard’ for all controllers in  
modules, but the controller MUST exist in a subddirectory. From what I  
can gather, it sounds like you’re looking to avoid the requirement  
that such a controller is in an ‘active\_rbac’ subdirectory.

I suspect that doing this would require the engines plugin to provide  
a fairly complete alternative routing system, and  
auto-dependency-loading mechanism too, since this type of behaviour  
isn’t supported by what Rails provides _at all_. For me, the price of  
having to create a few subfolders is by far outweighed by the hassle  
of having to develop and then support a whole new dependency layer.

> > Do you by chance know where to look for that magic in Rails? I want  
> > to have a look and could drop half an our searching if you know it  
> > off your head.

To understand what Rails is doing when it loads controllers, you need  
to start by looking at the following files:

activesupport/lib/active\_support/dependencies.rb  
actionpack/lib/action\_controller/routing.rb (at the auto-generated  
recognize! method)

Personally, I’d like to see the amount of magic that the engines  
plugin has to perform actually reduce, rather than increase, since it  
makes it much more likely that we can reuse core behaviour. It also  
means that developers aren’t surprised by the behaviour they see, and  
can use their experience with ‘normal’ rails applications to greatest  
effect when developing engines. Principle of Least Surprise,  
basically.

Aesthetically, i completely sympathise with what you’re suggesting,  
and if the Rails dependency loading system could support something  
like that easily then it wouldn’t be an issue at all… but at the  
moment, there doesn’t seem to be a simple way of doing this.

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![mhutter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@mhutter](https://www.ruby-forum.com/u/mhutter)\
**Post date:** [February 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/controller-namespace-support/100647/10 "2006-02-03T18:58:39Z")

</div>

Do you mean that every class in the engine ‘MyEngine’ should be put  
into the module MyEngine?

i.e. class User \< ActiveRecord::Base automatically becomes class  
MyEngine::User \< ActiveRecord::Base ?

While something like this is superficially appealing, it’s actually  
way more complicated to implement (even Rails doesn’t support this  
yet) and IMHO adding ‘more magic’ at this point isn’t a brilliant  
idea.

When you’re writing a regular Rails application, any controllers that  
are within modules have to reside in a sub-folder of the  
app/controllers directory - there’s no way around that. This is  
mirrored directly in the way that Engines work - if you want your  
controllers to be in modules, you have to put them in subfolders; the  
fact they’re in an engine isn’t important.

The way that Rails controllers exist within Modules is pretty much a  
hack in Rails itself right now. The module is half there for reloading  
purposes, and half for routing (i.e. to let you put controllers under  
subdirectories on your site).

So to answer your point - it’s no more or no less elegant than if you  
were developing the controllers in your application directly. Only  
time will tell how things might be improved.

- james

On 2/3/06, Manuel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> IMO this is not exactly elegant. Shouldn’t all classes be prefixed by  
> [http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org)

–

- J \*  
~
