views:

23

answers:

2

We're migrating a site from a proprietary framework to Ruby on Rails (v2.3). The current framework sometimes puts /base/ at the start of the URL for no discernible reason, and I'd like the existing URL to work, even though we won't give it out any more.

My current solution, which I don't like, is to define the routes once on the main map and once on a 'base' scope:

def draw_routes(map)
  # do my routing here
end

ActionController::Routing::Routes.draw do |map|
  map.with_options :path_prefix => '/base' do |base|
    draw_map(base)
  end

  draw_map(map)
end

what I'd like to do is something like:

ActionController::Routing::Routes.draw do |map|
  map.strip 'base'

  # do my routing here
end

is there a solution of that form?

A: 

I think that you could simply do something like:

map.connect 'base/:controller/:action/:id'

That should route you to the right controller, action and id.

Yannis
ah - should have said, I'm not using the standard :controller/:action/:id routes. I have a long list of map.connect statements that I don't want to have to duplicate.
Simon
+2  A: 

You can write a middleware to remove the base from the url. In lib/remove_base.rb:

class RemoveBase
  def initialize(app)
    @app = app
  end

  def call(env)
    env['REQUEST_PATH'].gsub!(/^\/base/, '')
    env['PATH_INFO'].gsub!(/^\/base/, '')
    env['REQUEST_URI'].gsub!(/^\/base/, '')
    @status, @headers, @response = @app.call(env)
    [@status, @headers, self]
  end

  def each(&block)
    @response.each(&block)
  end
end

and add this line in config/environment.rb

config.middleware.use "RemoveBase"

I've tested it in 2.3.8 with mongrel, and it seems to work.

hellvinz