Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the replacement for ActionController::Base.relative_url_root?

Tags:

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 up the following in our foo-app production config to ensure that our foo-app routes worked properly:

ActionController::Base.relative_url_root = "/foo-app"

However, with rails3, I now get:

DEPRECATION WARNING: ActionController::Base.relative_url_root is ineffective. Please stop using it.

I have since changed the config entry to the following:

config.action_controller.relative_url_root = "/foo-app"

This mostly works in that all calls to external resources (javascript/css/images) will use /foo-app. However, none of my routes change appropriately, or put another way, foo-app root_path gives me '/' when I would expect '/foo-app'.

Two questions:

  1. What is the replacement for ActionController::Base.relative_url_root
  2. if it is config.action_controller.relative_url_root, then why are my routes not reflecting the relative_url_root value I set?
like image 907
ynkr Avatar asked Jul 05 '10 19:07

ynkr


2 Answers

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)

like image 110
rizzah Avatar answered Sep 20 '22 00:09

rizzah


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.

like image 28
Christoph Petschnig Avatar answered Sep 20 '22 00:09

Christoph Petschnig