Changing the base URL for Rails 3 development
Asked Answered
B

2

15

I know I'm going to deploy to an environment with my application running with a base URL which looks like this:

http://someserver/mydepartment/myapp

My development environment is set up to use the default Rails configuration, which looks like this:

http://localhost:3000/myapp

I'd like to model this deployment path in my development environment. That is, I'd like to develop with a base URL which looks like this:

http://localhost:3000/mydepartment/myapp

That way, I can make all my URLs relative to "/" and they will work in both environments.

How can I change it so my application will live at this path in my development environment?

Solutions I've found, but don't work for me:

  • Setting the scope in routes.rb doesn't seem to work for the static content in public.
  • Using Apache's rewriting capabilities. I don't want to install Apache on my development box. Ideally the solution would work with WEbrick, though I seem to have Mongrel mostly working as well (there are some problems with Mongrel and Ruby 1.9.2).
  • Setting relative_url_root and similar suggestions which don't work with Rails 3.
  • Dynamically generating CSS/JavaScript and adjusting the paths to compensate between development and production environments.
Bullington answered 5/4, 2011 at 21:41 Comment(4)
What Operating system are you using for Development? mac/linux/windowsByars
My development OS is Windows.Bullington
How is it being deployed in production? I'd try and do that locally.Ambient
They use Passenger, which doesn't run on Windows.Bullington
B
16

You can try mapping your rails app rack config to a different base_uri. All you need to do is wrap the existing 'run' command in a map block

try doing this in your rails 'config.ru' file:

map '/mydepartment' do
    run Myapp::Application
end

Now when you 'rails server' the app should be at localhost:3000/mydepartment . Not sure if this will give you the desired outcome, but worth a try.

Byars answered 14/4, 2011 at 6:7 Comment(4)
I had to use "map '/mydepartment/myapp' do", i.e. adding "/myapp", but this did the trick. Thanks so much for your help, Barlow!Bullington
Hi @Barlow, I have a similar situation, but I'm wondering if there is a way to dynamically map a Rails app at run-time? I've created a question for this and would appreciate any response: #11060562Undersexed
Just a note, this doesn't work in Rails 4. The views can be found, but the assets still get mapped to the root when using the asset url/path helpers. The server can't find the assets at the root url, though.Spermatophore
@Spermatophore What should I do for rails 4? =DCapitol
A
6

Here’s how you can deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.

In config/routes.rb:

scope 'my_subdir' do
  # all resources and routes go here
end

In your Apache configuration file:

Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
  SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
  PassengerAppRoot /var/www/my_subdir
</Location>

And it should work, including automatically pointing all your assets to /my_subdir.

Altruism answered 9/9, 2011 at 21:57 Comment(1)
This worked for me using Rails 3.2.12 without modifying my routes.rbPlow

© 2022 - 2024 — McMap. All rights reserved.