config.action_controller.relative_url_root doesn't work in Rails 3.1 #4308

Closed
rustyx opened this Issue Jan 4, 2012 · 16 comments

Comments

Projects
None yet

rustyx commented Jan 4, 2012

According to documentation, there is a configuration option config.action_controller.relative_url_root:
http://guides.rubyonrails.org/configuring.html#configuring-action-controller

But when I set config.action_controller.relative_url_root, I get:

<NoMethodError: undefined method `relative_url_root=' for ActionController::Base:Class>
org/jruby/RubyKernel.java:2097:in `send'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/railtie.rb:37:in `Railtie'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/ordered_hash.rb:162:in `each'
org/jruby/RubyArray.java:1612:in `each'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/ordered_hash.rb:162:in `each'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/railtie.rb:37:in `Railtie'
org/jruby/RubyKernel.java:2062:in `instance_eval'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
org/jruby/RubyArray.java:1612:in `each'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/base.rb:234:in `Base'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/base.rb:171:in `ActionController'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/base.rb:3:in `(root)'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_controller/base.rb:31:in `ext'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_dispatch/middleware/static.rb:15:in `match?'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/actionpack-3.1.3/lib/action_dispatch/middleware/static.rb:47:in `call'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/railties-3.1.3/lib/rails/engine.rb:456:in `call'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/content_length.rb:14:in `call'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/railties-3.1.3/lib/rails/rack/log_tailer.rb:14:in `call'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/handler/mongrel.rb:77:in `process'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:159:in `process_client'
org/jruby/RubyArray.java:1612:in `each'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:158:in `process_client'
C:/java/jruby-1.6.5.1/lib/ruby/gems/1.8/gems/mongrel-1.1.5-java/lib/mongrel.rb:285:in `run'
org/jruby/RubyProc.java:270:in `call'
org/jruby/RubyProc.java:224:in `call'

I have the same problem: undefined method 'relative_url_root='.
I'm using RAILS 3.1.3, ruby 1.9.3 and thin.

Is the doc at http://guides.rubyonrails.org/configuring.html#configuring-action-controller incorrect? If the doc is incorrect it should be updated to indicate a simple way to configure an environment with a relative root url.

Contributor

dpickett commented Jan 15, 2012

I can confirm this error when adding the following to config/application.rb

  config.action_controller.relative_url_root = "/foo"

it appears to be a configuration option on Rails::Application proper.

https://github.com/rails/rails/blob/master/railties/lib/rails/application/configuration.rb#L40

Setting config.relative_url_root to foo did not raise an error when bringing up a server but it does not appear to have any effect on the url or path helpers. Oddly enough at runtime, I can assign a relative url root off of action controller. Again, it does not appear to have an effect.

ruby-1.9.2-p180-patched :016 > ApplicationController.config.relative_url_root = "/foo"
 => "/foo" 
ruby-1.9.2-p180-patched :017 > root_path
 => "/" 
ruby-1.9.2-p180-patched :018 > include ExampleApp::Application.routes.url_helpers
 => Object 
ruby-1.9.2-p180-patched :019 > root_path
 => "/" 

My guess is that this is in fact a regression and should be repaired. The only area we look to have test coverage around this functionality is in asset management, which would not affect named routes. Either this guide is misleading in that this is only useful for asset paths, or this is a regression

Contributor

parndt commented Mar 12, 2012

@dpickett is yours the same issue as #4820 and #5122 ?

julian7 commented Mar 12, 2012

There is no correlation. #4820 is a no method error issue, while #5122 is that relative_url_root is not honored in url_for. @dpickett's problem is clearly a #5122 one.

Contributor

ndbroadbent commented Mar 16, 2012

I am not having this issue with Rails 3.2.2. We use a Setting model that configures the base_url in application.rb and routes.rb:

# config/application.rb
module My
  class Application < Rails::Application
    config.action_controller.relative_url_root = Setting.base_url
    ...


# config/routes.rb
My::Application.routes.draw do
  scope Setting.base_url do
    ...

This is working fine, and the asset pipeline generates URLs like /foo/assets/screen.css, instead of /assets/screen.css.

julian7 commented Mar 16, 2012

@ndbroadbent If this is the way of using relative_url_root, then this is a failure at the drawing table. When you have to deploy to a subdirectory (or you have any deployment detail), you don't want to creep that into your development. This "solution" is a workaround at most.

Contributor

ndbroadbent commented Mar 16, 2012

Sorry, no, this is not really a solution for you. I'm just demonstrating that config.action_controller.relative_url_root is working for us, with assets and routes behaving correctly.

Contributor

kytrinyx commented Jun 30, 2012

I think this is related to change 87c57bb @josevalim do you know anything about this?

Contributor

pedz commented Jul 4, 2012

I'm on rails 3.2.3, ruby 1.9.3p194. I've been searching and trying things all day today.

First, env['SCRIPT_NAME'], set by the server is key here. thin understands --prefix but WEBrick appears to not understand it.

With SCRIPT_NAME set to /subdir things being to work. My method was to use thin with the --prefix argument. I have an apache 2.2 front end. I don't know if apache is setting SCRIPT_NAME and PATH_INFO or if thin is. But between the two of them, by the time the request reaches my controller, both are set as I want them.

A call to named path foo_path(item) produces the correct path.

However, assets are (half) left out in the cold. They are accessed by /subdir/assets/... as they should be but the generated paths for the script tag start with "/assets/...". To fix this I added:

config.action_controller.relative_url_root = '/subdir'

(in my case to config/environments/development.rb because I just need it for development on my laptop). This, to me, is not a fix. If named routes can figure out how to do things right with SCRIPT_NAME, then Sprockets / assets should be able to as well.

--- update ---
thin with --prefix by itself is doing most of the magic when it does:

    server.app = Rack::URLMap.new(@options[:prefix] => server.app) if @options[:prefix]

I can run thin without the apache front end to verify that. This gets SCRIPT_NAME operating so that most of the rest of the code, with the exception of assets, works.

ActionView::AssetPaths is using relative_url_root which is why setting it is needed to get assets to work (see actionpack-3.2.3/lib/action_view/asset_paths.rb).

There seem to be lots of related issues with having an app live in a sub directory. IMO, config.relative_url_root should be the master, and both the controller, routing table, and asset pipeline (i.e. Sprockets) should use it if set. And if config.relative_url_root isn't set, but ENV['RAILS_RELATIVE_URL_ROOT'] is set, then it should use that. It seems silly to have multiple ways to tell the application that it lives in a sub directory.

EDIT

Clarification: So far I've seen references to fixing this issue by modifying config.relative_url_root, config.action_controller.relative_url_root, config.assets.prefix, env['RAILS_RELATIVE_URL_ROOT'], and now env['SCRIPT_NAME']. Surely one of these should be considered the authority when it comes to specifying a root path and the rest could inherit from that.

EDIT 2

Also seems that these settings are applied differently depending on how config.assets.initialize_on_precompile is set, and that in some environments, such as Heroku, "environment variables aren't present during asset compilation". So again, one of those config files should be authoritative.

ddnexus commented Nov 10, 2013

@chrisbloom7: Absolutely true! That is a big mess (and also in rails 4 AFAIK).
Why is this still an open issue after so long?

Member

robin850 commented Dec 8, 2013

Hello world,

Could someone gives us a way to reproduce this problem or upload a sample application that demonstrate the problem please ? I'm unable to reproduce the problem with the following:

  • Rails 3.1.3 or 4.0.2
  • WEBrick or Thin
  • Ruby 2.0.0 or JRuby 1.7.6
Member

drogus commented Dec 29, 2013

I'm closing due to inactivity. I'll reopen if there is an easy way to reproduce it.

@drogus drogus closed this Dec 29, 2013

Contributor

pedz commented Dec 29, 2013

Ok. I meant to put together a testcase for you but didn't find time.

Perhaps its fixed. I'm surprised it was hard to recreate.

On Dec 29, 2013, at 10:14 AM, Piotr Sarnacki notifications@github.com wrote:

I'm closing due to inactivity. I'll reopen if there is an easy way to reproduce it.


Reply to this email directly or view it on GitHub.

Member

drogus commented Dec 29, 2013

@pedz feel free to upload a testcase or an app, I'm not saying this is not an issue anymore, but I see some confusion in comments above and the issue is 2 years old. A clear and up to date way to reproduce would be great if someone wants to look into fixing it.

I'm very confused as to the recommended way to mount a rails app in a sub-directory.

What I've had success with is setting both the MyApp::Application.config.relative_url_root in application.rb and wrapping the run App command in config.ru:

map MyApp::Application.config.relative_url_root || "/" do
  run Rails.application
end

However, it does not work in test, the sub-directory is not utilized, perhaps because config.ru is not used by minitest / rspec when loading the rails environment? When Devise redirects to the sign in page, I get a RouteError because route /path-to/users/sign-in does not work (this suggests Devise does use the sub-directory somehow but the rest of the Rails env doesn't...).

I've tried the alternative of wrapping the routes in a scope and while it appears to work in dev, I cannot make the assets to load.

A definitive guide would be most welcome.

joshua-whittemore added a commit to joshua-whittemore/calagator that referenced this issue Nov 10, 2014

- delete cruft from config.ru
- make another absolute href relative
- if env variable RAILS_RELATIVE_URL_ROOT not set, make default app location '/'
- on local machine, assets map correctly to the subdirectory when RAILS_RELATIVE_URL_ROOT specified, e.g.:

$ RAILS_RELATIVE_URL_ROOT='/foo' rails server -p 3000

But not certain it will work in production.  Lots of problems listed here: rails/rails#4308

- still need a test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment