Damon McCormick
2006-Feb-13 10:09 UTC
[Rails] dynamically generating a controller and appropriate routing
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/e4c82930/attachment.html
Steve Longdo
2006-Feb-13 16:40 UTC
[Rails] dynamically generating a controller and appropriate routing
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 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. On 2/13/06, Damon McCormick <damon.mccormick@gmail.com> wrote:> > 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060213/879fb165/attachment.html