views:

979

answers:

3

When i'm starting the server with the path option

 script/server --path=/myapp

while having a route

 map.route 'foo', :controller => 'bar', :action => 'buzz'

then

ActionController::Routing::Routes.recognize_path('/myapp/foo')

raises an error "No route matched ..."

Question: How can i make Rails built-in routing recognize with path prefix? Thanks a lot!

A: 

There is actually a path_prefix available for routes so you can do something like this:

map.foo, 'foo', :controller => 'bar', :action => 'buzz', :path_prefix => 'myapp'

That should give you a route for /myapp/foo

paulthenerd
+2  A: 

Try putting config.action_controller.relative_url_root = "/myapp" in environments.rb and start your server normally.

Then Rails will append /myapp/ to all your routes

A: 

hi,

thanks a lot for your answers!

unfortunately i can't use the :path_prefix option in map.foo, because it's not always the case (the end-user should be responsible for setting or not the prefix while not worring about any routes).

i fingered out following:

path = '/myapp/foo'

if relative_url_root = ActionController::Base.relative_url_root
  path.sub!(/\A#{relative_url_root}/i, '')
end

params = ActionController::Routing::Routes.recognize(path)
# => {:controller => 'bar', :action => 'buzz'}
kostia