Forum: Ruby on Rails dynamically generating a controller and appropriate routing

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Damon McCormick (Guest)
on 2006-02-13 12:10
(Received via mailing list)
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:


Does anyone have any suggestions as to how I could go about getting the
route recognized? Any help is greatly appreciated.
Steve L. (Guest)
on 2006-02-13 18:40
(Received via mailing list)
I sent a query on a similar issue awhile ago ( )

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.
This topic is locked and can not be replied to.