Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Routing with dynamic root uri


  • Please log in to reply
2 replies to this topic

#1 dansmood

dansmood

    Passenger

  • Members
  • 3 posts

Posted 08 November 2013 - 05:54 PM

What I want to do has probably been done before:

  • Have httpd reverse proxy to a particular port with a particular port, similarly to the way you do with unicorn
  • Have the root uri be configurable for different environments
  • The goal is to have developers be able to show their work in a proxied sub-path on port 80:

RewriteEngine on


RewriteRule ^/dan$ /dan/ [R]
<Location /dan1>
    ProxyPass http://localhost:3004/dan/
    ProxyPassReverse http://localhost:3004/dan/
</Location>

 

Well, that works with the asset pipeline:

 

config.relative_url_root = '/whatever'

 

But it works less well in routing:

 

Some::Application.routes.draw do
  scope "dan" do
    root :to => redirect('/dan/home')
    get "home" :to => "home#index"
  end
end

 

What is the best way to make this scoping code dynamic and simple to maintain?



#2 dansmood

dansmood

    Passenger

  • Members
  • 3 posts

Posted 08 November 2013 - 06:06 PM

Putting the following in config/routes.rb works fine:

 

Some::Application.routes.draw do

  dynamic_scope = ENV['RAILS_SCOPE']
  scope dynamic_scope do
    root :to => redirect('/dan/home')
    get "home" :to => "home#index"
  end
end

 

Rails does the right thing if the scope is nil

 

I also put this in config/application.rb (because RAILS_RELATIVE_URL_ROOT is too long for me to type often):

 

config.relative_url_root = ENV['RAILS_SCOPE']



#3 dansmood

dansmood

    Passenger

  • Members
  • 3 posts

Posted 08 November 2013 - 06:37 PM   Best Answer

Okay - I guess the mini-app I was trying this on did not have any assets.   I should also leave environment variables starting with RAILS_ for Rails proper:

 

Some::Application.routes.draw do

       dynamic_scope = ENV['MY_URL_ROOT']
       scope dynamic_scope do

                   ....

 

To get assets to work, image_tag and similar functions need to work (config.action_controller.relative_url_root) and sprockets has to respond to the correct prefix.   My solution was to place the following in config/application.rb:

 

if ENV['MY_URL_ROOT']

config.assets.prefix = ENV['MY_URL_ROOT'] + '/assets'

config.action_controller.relative_url_root = ENV['MY_URL_ROOT']

end


  • Ohm likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users