Dynamically generating a controller and appropriate routing


I’m working on a library similar to acts_as_taggable. I’d like it to be
simple, one file drop-in: i.e., put my_library.rb into RAILSROOT/lib/
have everything work. However, the library requires a controller to
some stuff, so I need to be able to create the controller class, set the
routing, and extend the controller at run-time from within

I have code that creates and extends the controller properly; I’ve
that it works via the console. However, the code that I have in place to
a route to connect a url to the extended methods in the controller
seem to take effect. When testing via the webrick server, I get an
unrecognized route error.

What I have in my_library.rb is something like the following:

say I’ve generated the following controller from within my_library.rb:

class ASampleController < ActionController::Base
def some_thing
render_text “hello world!”

and also from within my_library.rb I have the following code to add a

ActionController::Routing::Routes.connect ‘/show/:model_name/:id’,
:controller => ‘ASampleController’, :action => :model_name.to_s

However, the route doesn’t seem to be put in place, or even recognized.
If I
examine ActionController::Routing::Routes from within the console, my
does not show up. When I attempt to browse to the appropriate url, I get
following in log/development.log:


recognition_failed' /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:459:inrecognize!’

Does anyone have any suggestions as to how I could go about getting the
route recognized? Any help is greatly appreciated.


I sent a query on a similar issue awhile ago (
http://wrath.rubyonrails.org/pipermail/rails/2006-February/016706.html )

The Restafarian plugin includes a module that seems to add a method to
routes at runtime:

ActionController::Routing::RouteSet.send :include, RestfulRoutes

So you could probably include a method to add a named_route as above and
then call it in init.rb. There is however a reload method that forces a
re-read of routes.rb defined in routing.rb in the rails core so I am
guessing that it gets called at least once… It would be fantastic if
someone could confirm the behavior of this reload method in routes.