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

Merged
merged 1 commit into from Sep 13, 2011

5 participants

@guilleiguaran
Ruby on Rails member

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

See rails/sass-rails#42 for details

@nbarthelemy

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
      protected
      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
        end
        path
      end
    end
  end
end

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

@guilleiguaran
Ruby on Rails member

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

@nbarthelemy

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

Rake::Task["environment"].invoke
@nbarthelemy

I can confirm it works. Nice fix :)

@guilleiguaran
Ruby on Rails member

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

@spastorino spastorino merged commit b0cee70 into rails:master Sep 13, 2011
@markembling

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?

@guilleiguaran
Ruby on Rails member

@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
@markembling

@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 :/

@spastorino
Ruby on Rails member

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

@markembling

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