Rails relative_url doesnt adjust links
Asked Answered
E

3

7

After failed attempts with Passenger (see my other question), I managed to get rails running via a reverse proxy in a subfolder. I added the lines

config.relative_url_root = "/App"
config.action_controller.relative_url_root = "/App"

to my environment. Now I can access my rails project under www.mySite.com/App. The problem is that the links /paths dont add the prefix "/App". So a link to "users" looks like www.mySite.com/users instead of www.mySite.com/App/users. How can I change this?

At least according to http://edgeguides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root I did everything correct.

Euchromatin answered 4/1, 2015 at 0:57 Comment(2)
Can you run rake routes in your terminal and post the output here?Meneses
Here it is pastebin.com/XWEjRZsiEuchromatin
E
3

I got it working by adjusting my reverse proxy and my rails config as follows. This is my corresponding apache file:

<VirtualHost *:80>
DocumentRoot /path/to/App/public
ProxyPass /App http://127.0.0.1:9292/App
ProxyPassReverse /App http://127.0.0.1:9292/App
</VirtualHost>

My config.ru looks like this:

require ::File.expand_path('../config/environment',  __FILE__)
map '/App' do
  run Rails.application
end

The mentioned environments variables are set in /config/environment.rb. I am not sure if they are still needed:

config.relative_url_root = "/App"
config.action_controller.relative_url_root = "/App"
ENV['RAILS_RELATIVE_URL_ROOT']  = "/App"
ENV['ROOT_URL']  = "/App"
Euchromatin answered 4/1, 2015 at 14:13 Comment(2)
Thanks for this. Did you ever figure out the optimal configuration? I have exactly the same problem where the asset URLs are fine but the links are not prefixed by the sub-app. I am using nginx with Puma.Bratislava
I tried several configurations based on the given solutions. I found two minimal solutions: either just config.relative_url_root = "/App", or config.action_controller.relative_url_root = "/App". The file config.ru has to be modified... . Using environment variables, I could just get to pages, but assets (images, js, and css) where not handled...Obtuse
I
2

In Rails 4.2, I had the same need and resolved modifying some configuration files. Routes.rb:

Rails.application.routes.draw do
  scope :mytm do
     ....
  end 
end 

This do not require any modification to the application.

Furthermore, you have to reconfigure the asset pipeline, in /config/initializers/assets.rb:

  Rails.application.config.assets.prefix = "/app/asset"
  ....
Intend answered 21/8, 2016 at 21:29 Comment(0)
M
0

I'm not entirely sure if I understand what you're trying to do, but I think what you're describing is essentially namespacing all of your controllers under "/App" or something like that. This would be in your routes.rb file:

namespace "App" do

  # Put your resources here, as however you defined them before, e.g.:  
  resources :users, :decks, :cards, :revs, :sessions

end
Meneses answered 4/1, 2015 at 2:16 Comment(1)
I tried this before; doing this results in App/users is not a supported controllerEuchromatin

© 2022 - 2024 — McMap. All rights reserved.