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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D62f407729fe5a5e33b53d571d43df2a?d=identicon&s=25 Damon McCormick (Guest)
on 2006-02-13 11:10
(Received via mailing list)
I'm working on a library similar to acts_as_taggable. I'd like it to be
a
simple, one file drop-in: i.e., put my_library.rb into RAILSROOT/lib/
and
have everything work. However, the library requires a controller to
render
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
my_library.rb.

I have code that creates and extends the controller properly; I've
tested
that it works via the console. However, the code that I have in place to
add
a route to connect a url to the extended methods in the controller
doesn't
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!"
  end
end

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

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
route
does not show up. When I attempt to browse to the appropriate url, I get
the
following in log/development.log:

    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:469:in
`recognition_failed'
    /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:459:in
`recognize!'
    /opt/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in
`dispatch

Does anyone have any suggestions as to how I could go about getting the
route recognized? Any help is greatly appreciated.
5ce55767684e0cd1727db2b8b8975640?d=identicon&s=25 Steve Longdo (Guest)
on 2006-02-13 17:40
(Received via mailing list)
I sent a query on a similar issue awhile ago (
http://wrath.rubyonrails.org/pipermail/rails/2006-... )

The Restafarian plugin includes a module that seems to add a method to
add
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.