Add routes with a rails plugin or gem
It is possible to define routes in a ruby on rails plugin or gem. Normally, adding routes looks like this:
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
The problem is that the draw method clears the existing routes before adding the new ones (Ruby on Rails 1.2.3: routing.rb):
def draw
clear!
yield Mapper.new(self)
named_routes.install
end
The plugins and gems are loaded first, so any new routes are cleared when config/routes.rb is loaded. One solution is to redefine the clear! method to do nothing:
class << ActionController::Routing::Routes;self;end.class_eval do
define_method :clear!, lambda {}
end
The final result should be included in the plugin or gem:
class << ActionController::Routing::Routes;self;end.class_eval do
define_method :clear!, lambda {}
end
ActionController::Routing::Routes.draw do |map|
map.connect 'newurl', :controller => 'plugin_controller', :action => 'some_action'
end