# Problems with engine loading and routes w\\ Rails 2.0

**URL:** <https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833>\
**Category:** Engines Development\
**Created:** [December 12, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833 "2007-12-12T18:20:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/1 "2007-12-12T18:20:39Z")

</div>

I’m trying to map.from\_plugin :my\_engine in the routes.rb for my  
application

The problem appears to be that at the time routes.rb has been processed  
the  
Engines plugin itself is loaded but my Engine has not.

On line 69 of lib/engines/rails\_extensions/routing.rb:

routes\_path = Engines.plugins[name].routes\_path

Engines.plugins[name] is returning nil for my engine since it hasn’t  
been  
loaded yet, and it dies trying to call nil.routes\_path. Perhaps this  
should  
raise ArgumentError or something if Engines.plugins[name] is nil…

At the time routes.rb is being processed, there’s only one entry in  
Engines.plugins:

#\<Engines::Plugin:0x1945934 @public\_directory=nil,  
@controller\_paths=[“app/controllers”, “components”], @loaded=true,  
@code\_paths=[“app/controllers”, “app/helpers”, “app/models”,  
“components”,  
“lib”], @name=“engines”,  
@directory="/Users/tony/src/clickcaster/vendor/plugins/engines"\>

Is there some way I can explicitly load my engine in environment.rb  
until  
this problem can be corrected?

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/2 "2007-12-12T20:34:03Z")

</div>

On Dec 11, 2007 9:31 PM, Tony A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to map.from\_plugin :my\_engine in the routes.rb for my application
> 
> The problem appears to be that at the time routes.rb has been processed the  
> Engines plugin itself is loaded but my Engine has not.

If your plugin hasn’t been loaded yet, are you sure it ever loads?  
Routing is not initialized until _after_ all the plugins have been  
loaded (see line 105 vs 114 of initializer.rb), so it doesn’t seem to  
make sense that your plugin wouldn’t be in the Engines.plugins  
structure. Does it definitely have a lib and/or init.rb file?

Regarding your implementation ideas, they would side-step the plugin  
loading process. I’d consider making Routes#from\_plugin do nothing if  
the plugin could not be found, since that would gracefully work as you  
removed plugins from config.plugins, but that still wouldn’t help your  
problem.

The key here is why your plugin isn’t present in Engines.plugins at  
the point the routes file is loaded. Could you pepper the init.rb  
files in your app with puts statements so we can trace what’s going  
on?

Cheers,

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/3 "2007-12-12T20:45:51Z")

</div>

On Dec 12, 2007 2:21 AM, James A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If your plugin hasn’t been loaded yet, are you sure it ever loads?

Yep. Everything works perfectly except map.from\_plugin…

Is it working for other people?

Routing is not initialized until _after_ all the plugins have been

> loaded (see line 105 vs 114 of initializer.rb), so it doesn’t seem to  
> make sense that your plugin wouldn’t be in the Engines.plugins  
> structure. Does it definitely have a lib and/or init.rb file?

It had a lib but no init.rb. Adding an empty init.rb didn’t fix the  
problem.

Regarding your implementation ideas, they would side-step the plugin

> loading process. I’d consider making Routes#from\_plugin do nothing if  
> the plugin could not be found, since that would gracefully work as you  
> removed plugins from config.plugins, but that still wouldn’t help your  
> problem.
> 
> The key here is why your plugin isn’t present in Engines.plugins at  
> the point the routes file is loaded. Could you pepper the init.rb  
> files in your app with puts statements so we can trace what’s going  
> on?

Here’s what I see if I have my workaround in place (just starting  
script/console). Added some print statements to initializer.rb too:

$ script/console  
Loading development environment (Rails 2.0.1)  
load\_environment  
load\_plugins  
Engines init.rb called  
Engines.init complete  
My engine’s init.rb called  
initialize\_routing  
after\_initialize

> >

Without the workaround in place:

$ script/console  
Loading development environment (Rails 2.0.1)  
load\_environment  
load\_plugins  
Engines init.rb called  
Engines.init complete  
/Users/tony/src/clickcaster/vendor/plugins/engines/lib/engines/rails\_extensions/routing.rb:69:in  
`from\_plugin’:NoMethodError: You have a nil object when you didn’t  
expect  
it!

So it’s dying somewhere in load\_plugins, before my engine’s init.rb gets  
loaded…

I’ll keep digging…

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/4 "2007-12-12T21:18:34Z")

</div>

Or for that matter…

class ActionController::Routing::RouteSet::Mapper  
def from\_plugin(name)  
eval File.read(File.join(RAILS\_ROOT,  
“vendor/plugins/#{name}/routes.rb”))  
end  
end

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/5 "2007-12-12T22:05:39Z")

</div>

Hi Tony,

Am 12.12.2007 um 03:53 schrieb Tony A.:

> Or for that matter…
> 
> class ActionController::Routing::RouteSet::Mapper  
> def from\_plugin(name)  
> eval File.read(File.join(RAILS\_ROOT, “vendor/plugins/#{name}/  
> routes.rb”))  
> end  
> end

plugins are allowed to be located in arbitrary subdirectories under  
vendor/plugins.

So vendor/plugins/this/is/a/valid/plugin is a valid plugin dir, if  
there’s either an init.rb or a lib subdirectory in it.

> ```
> def from_plugin(name)
> 
> ```
> 
> @code\_paths=[“app/controllers”, “app/helpers”, “app/models”,
> 
> Tony A.  
> ClickCaster, Inc.  
> removed\_email\_address@domain.invalid \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Engine-Developers mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org)

–  
sven fuchs removed\_email\_address@domain.invalid  
artweb design [http://www.artweb-design.de](http://www.artweb-design.de)  
grünberger 65 + 49 (0) 30 - 47 98 69 96 (phone)  
d-10245 berlin + 49 (0) 171 - 35 20 38 4 (mobile)

---

<div class="post-metadata">

**Author:** ![murfhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d26b3c/32.png) [@murfhy](https://www.ruby-forum.com/u/murfhy)\
**Post date:** [December 12, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/problems-with-engine-loading-and-routes-w-rails-2-0/122833/6 "2007-12-12T20:57:43Z")

</div>

Is there any reason why map.from\_plugin needs to be any more complex  
than  
this:

module ActionController  
module Routing  
class RouteSet  
class Mapper  
def from\_plugin(name)  
eval File.read(File.join(RAILS\_ROOT,  
“vendor/plugins/#{name}/routes.rb”))  
end  
end  
end  
end  
end

That’s certainly working fine for me for the time being…
