I am porting a 2.x rails app to rails3; we\'ll call it foo-app. Foo-app is one section of a larger rails app and lives at main_rails_app.com/foo-app. Previously we just set
You should be able to handle all that within the routes.rb file. Wrap all your current routes in scope; for instance.
scope "/context_root" do
resources :controller
resources :another_controller
match 'welcome/', :to => "welcome#index"
root :to => "welcome#index"
end
You can then verify your routing via the rake routes
they should show your routes accordingly, including your context root(relative_url_root)
If you deploy via Passenger, use the RackBaseURI
directive: http://www.modrails.com/documentation/Users%20guide%20Apache.html#RackBaseURI
Otherwise, you can wrap the run
statement in your config.ru
with the following block:
map ActionController::Base.config.relative_url_root || "/" do
run FooApp::Application
end
Then you only have to set the environment variable RAILS_RELATIVE_URL_ROOT
to "/foo-app". This will even apply to routes set in gems or plugins.
Warning: do not mix these two solutions.
I feel like I must be over-complicating this and/or missing something, but this issue has been frustrating me for a while now, and here are my notes.
There are two separate issues with two points each for dynamic and static routes:
One way to solve all four points:
relative_root
portion
/
like developmentRAILS_RELATIVE_URL_ROOT
environment variable
ScriptName
middleware below (modify it to use the value from the environment)
users_path
# config.ru
map '/relative_root' do
run Myapp::Application
end
SCRIPT_NAME
rack/urlmap.rb:62SCRIPT_NAME
to url_for options metal/url_for.rb:41So that covers URLs generated by the url helpers, e.g. given UserController
, users_path
will be prefixed by the relative url root.
# config.ru
class ScriptName
def initialize(app, name)
@app = app
@name = name
end
def call(env)
env['SCRIPT_NAME'] += @name
@app.call(env)
end
end
use ScriptName, '/relative_root'
run Rails.application
app.config.relative_url_root
configuration.rb:41config.action_controller.relative_url_root
/relative_root
(rizzah's answer)# config/routes.rb
Myapp::Application.routes.draw do
scope '/relative_root' do
...
end
end
/relative_root/images/logo.png
will result in "no route matches" exceptions. This may not be an issue if nginx is serving static assets anyway.Given a config like this:
upstream myapp {
server localhost:3000;
}
server {
...
location /relative_root {
proxy_pass http://myapp/;
}
}
Nginx will strip out the /relative_root
, and the Rails app will not see it. If you need the Rails app so see it, one way is to change the proxy_pass
line:
...
proxy_pass http://myapp/relative_root/;
...