Tutorial :Server prefix and rails routes



Question:

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!


Solution:1

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


Solution:2

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


Solution:3

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'}  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »