Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Set relative url root in assets when controller isn't available for Sprockets #2977

merged 1 commit into from

5 participants

Guillermo Iguaran Nicholas Barthelemy Chris Eppstein Mark Embling Santiago Pastorino
Guillermo Iguaran

Set relative url root in assets when controller isn't available for Sprockets. Fix #2435

See rails/sass-rails#42 for details

Nicholas Barthelemy

I will give it a shot. I did something similar in sass-rails, but I definitely believe the fix should be in the Sprockets RailsHelper

module Sass
  module Rails
    module Helpers
      def public_path(asset, kind)
        resolver = options[:custom][:resolver]
        asset_paths = resolver.context.asset_paths
        path = resolver.public_path(asset, kind.pluralize)
        if !asset_paths.send(:has_request?) && ENV['RAILS_RELATIVE_URL_ROOT']
          path = ENV['RAILS_RELATIVE_URL_ROOT'] + path

I will check out your fix and get back to you shortly.

Guillermo Iguaran

@nbarthelemy, I think I should set the ENV["RAILS_RELATIVE_URL_ROOT"] also in assets:precompile rake task, right?

Nicholas Barthelemy

I believe it loads the environment.rb file, so you wouldn't need to. It is on line 11 of assets.rake.

Nicholas Barthelemy

I can confirm it works. Nice fix :)

Guillermo Iguaran

This will need some research because looks like the relative_url_root behavior is deprecated

Santiago Pastorino spastorino merged commit b0cee70 into from
Mark Embling

With this fix, how is it you tell it about what subdomain it'll be hosted under. I'm obviously missing something because I can't get it to work at all (Rails 3.1.1 with this fix). Help?

Guillermo Iguaran

@markembling the code was changed recently and I'm not sure about what you should do for it right now. Try one of those things and let me know what works for you:

  1. Setting config.action_controller.relative_url_root in an environment file (for example production.rb)
  2. Setting the env var RAILS_RELATIVE_URL_ROOT
  3. Setting Rails.application.config.action_controller.relative_url_root in an initializer
Mark Embling

@guilleiguaran I've given them a go:

  1. Definitely not. Crashes with 'undefined method `relative_url_root=' for ActionController::Base:Class'
  2. No luck there. Seems to be ignored.
  3. Same as 1.

This should not be this hard :/

Santiago Pastorino

I'd say that we should open a new issue to discuss that, and would be great if you can provide a sample app with detailed info to reproduce your problem. Thanks

Mark Embling

I've opened a new issue which hopefully explains the problem. I've had to pull out the relevant parts from my (non-public) app in order to illustrate it, so hopefully it's all there. Somewhat rough and ready though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 13, 2011
  1. Guillermo Iguaran
This page is out of date. Refresh to see the latest.
2  actionpack/CHANGELOG
@@ -48,6 +48,8 @@
*Rails 3.1.1 (unreleased)*
+* Set relative url root in assets when controller isn't available for Sprockets (eg. Sass files using asset_path). Fixes #2435 [Guillermo Iguaran]
* Fixed the behavior of asset pipeline when config.assets.digest and config.assets.compile are false and requested asset isn't precompiled.
Before the requested asset were compiled anyway ignoring that the config.assets.compile flag is false. [Guillermo Iguaran]
16 actionpack/lib/sprockets/helpers/rails_helper.rb
@@ -17,6 +17,7 @@ def asset_paths
paths.asset_digests = asset_digests
paths.compile_assets = compile_assets?
paths.digest_assets = digest_assets?
+ paths.relative_url_root = config.action_controller.relative_url_root
@@ -96,12 +97,17 @@ def asset_environment
class AssetPaths < ::ActionView::AssetPaths #:nodoc:
- attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets
+ attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets,
+ :digest_assets, :relative_url_root
class AssetNotPrecompiledError < StandardError; end
- def compute_public_path(source, dir, ext=nil, include_host=true, protocol=nil)
- super(source, asset_prefix, ext, include_host, protocol)
+ def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil)
+ public_path = super(source, asset_prefix, ext, include_host, protocol)
+ if !is_uri?(public_path) && relative_url_root
+ public_path = rewrite_relative_url_root(public_path, relative_url_root)
+ end
+ public_path
# Return the filesystem path for the source
@@ -149,6 +155,10 @@ def rewrite_extension(source, dir, ext)
+ def relative_url_root
+ has_request? ? super : @relative_url_root
+ end
7 actionpack/test/template/sprockets_helper_test.rb
@@ -124,6 +124,13 @@ def url_for(*args)
+ test "asset path with relative url root when controller isn't present but relative_url_root is" do
+ @controller = nil
+ @config.action_controller.relative_url_root = "/collaboration/hieraki"
+ assert_equal "/collaboration/hieraki/images/logo.gif",
+ asset_path("/images/logo.gif")
+ end
test "javascript path" do
assert_match %r{/assets/application-[0-9a-f]+.js},
asset_path(:application, "js")
Something went wrong with that request. Please try again.