Hey guys. In Rails 2, is there a way to create a route at run-time? I want to do this because a gem that I''m writing requires a single resource route, and I''d prefer that the gem create it at run-time instead of forcing the user to define the route in routes.rb . While trying to figure this out, I discovered that calling ActionController::Routing::Routes.draw do |map| # my gem''s route here end causes all of the routes in routes.rb to be removed. I tried copying the routes from ActionController::Routing::Routes into a new array or hash, then rebuilding them and adding my gem''s route, but couldn''t get that to work. Also, it''s messy and less than ideal. So, any suggestions? I''m all ears! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 28, 3:05 am, Nick Hoffman <n...-qGbiljoI0DQkmLvzuZlaBw@public.gmane.org> wrote:> Hey guys. In Rails 2, is there a way to create a route at run-time? I > want to do this because a gem that I''m writing requires a single > resource route, and I''d prefer that the gem create it at run-time > instead of forcing the user to define the route in routes.rb . > > While trying to figure this out, I discovered that calling > ActionController::Routing::Routes.draw do |map| > # my gem''s route here > end > causes all of the routes in routes.rb to be removed. > > I tried copying the routes from ActionController::Routing::Routes into > a new array or hash, then rebuilding them and adding my gem''s route, > but couldn''t get that to work. Also, it''s messy and less than ideal. >If you have a plugin with a structure that looks like my_plugin/ app/ controllers/ ... config/ routes.rb lib/ ... Then rails will add the routes from that routes.rb file. (Don''t know if you have to have app/controllers, i''ve just always wanted to add a route to a controller that was also in the plugin). Since plugins can be packaged as gems, some variation on the above should work Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 28, 3:17 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you have a plugin with a structure that looks like > > my_plugin/ > app/ > controllers/ > ... > > config/ > routes.rb > lib/ > ... > > Then rails will add the routes from that routes.rb file. (Don''t know > if you have to have app/controllers, i''ve just always wanted to add a > route to a controller that was also in the plugin). > > Since plugins can be packaged as gems, some variation on the above > should work > > FredInteresting! I didn''t that you could do that. Unfortunatey, I couldn''t get it to work. $cat config/routes.rb ActionController::Routing::Routes.draw do |map| map.resource :dashboard, :only => :show map.connect "dashboard/widgets/*path", :controller => :dashboards, :action => ''widget_data'' end $ $ cat vendor/gems/acts_as_dashboard-0.0.0/config/routes.rb puts "***** #{__FILE__}" ActionController::Routing::Routes.draw do |map| map.connect ''foo/*path'', :controller => :dashboards end $ $ rake routes (in /home/nick/src/acts_as_dashboard_container) dashboard GET /dashboard(.:format) {:action=>"show", :controller=>"dashboards"} /dashboard/widgets/:path {:action=>"widget_data", :controller=>"dashboards"} $ Any idea why the gem''s routes.rb isn''t being processed? Thanks! Nick -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I just found Section 5.4 in http://guides.rubyonrails.org/2_3_release_notes.html : "Another big change is that Rails now supports multiple routing files, not just routes.rb. You can use RouteSet#add_configuration_file to bring in more routes at any time – without clearing the currently- loaded routes." I''ll check this out tonight! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 28, 9:02 am, Nick <n...-qGbiljoI0DQkmLvzuZlaBw@public.gmane.org> wrote:> I just found Section 5.4 inhttp://guides.rubyonrails.org/2_3_release_notes.html > : > > "Another big change is that Rails now supports multiple routing files, > not just routes.rb. You can use RouteSet#add_configuration_file to > bring in more routes at any time – without clearing the currently- > loaded routes." > > I''ll check this out tonight!In addition to ActionController::Routing::RouteSet#add_configuration_file , there''s also #add_named_route , and simply #add_route . How convenient! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.