Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created August 15, 2011 09:44
Show Gist options
  • Save mindscratch/1145971 to your computer and use it in GitHub Desktop.
Save mindscratch/1145971 to your computer and use it in GitHub Desktop.
Rails (3.0.x): easily namespace routes for entire app
MyApp::Application.routes.draw do
my_draw = Proc.new do
get "widgets" => "widgets#index"
# add other routes here
end
# I chose to name my environment variable 'RAILS_RELATIVE_URL_ROOT' but it can
# be anything you want.
#
# In production I was deploying my Rails app (as a WAR, thanks to the warbler gem) to a Tomcat server.
# Tomcat associates applications to the name of the directory that gets created when a WAR file is
# is exploded, so if my WAR was named 'myapp.war' then I would end up with /path/to/tomcat/webapps/myapp
# which could be accessed via a browser at http://<server name>:<port>/myapp
#
# In development I wanted to test my app in a similar scenario but WEBrick (or whatever you use)
# doesn't work that way. So, I'd set the env. variable to '/myapp' and then when I ran my Rails app
# using "rails s"..I could access it as http://localhost:3000/myapp
if Rails.env != 'production' && ENV['RAILS_RELATIVE_URL_ROOT']
puts "drawing routes inside scope: #{ENV['RAILS_RELATIVE_URL_ROOT']}"
scope ENV['RAILS_RELATIVE_URL_ROOT'] do
my_draw.call
end
else
my_draw.call
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment