Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created January 11, 2012 16:15
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jrochkind/1595405 to your computer and use it in GitHub Desktop.
Rails 3.1.3 asset precompile with SubURI fix
# ./config/initializers/asset_precompile_prefix_fix.rb
#
# So we can deploy at a SubURI, and precompile assets to respect that with:
# RAILS_RELATIVE_URL_ROOT=/foo rake assets:precompile
#
# See: http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri
#
# Confirmed working in Rails 3.1.3
# Future versions of Rails may make this monkey patch unneccesary. (or break
# it without making it unneccesary)
#
#
# We are monkey patching source originally (in 3.1.3) at:
# https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/helpers/rails_helper.rb#L54
module Sprockets
module Helpers
module RailsHelper
included do |klass|
klass.alias_method_chain :asset_path, :prefix
end
def asset_path_with_prefix(*args)
path = asset_path_without_prefix(*args)
if !asset_paths.send(:has_request?)
path = ENV['RAILS_RELATIVE_URL_ROOT'] + path if ENV['RAILS_RELATIVE_URL_ROOT']
end
return path
end
end
end
end
@Taloncor
Copy link

Taloncor commented Feb 5, 2013

Is there anything else besides putting this file into the config/initializers directory and invoking asset:precompile with RAILS_RELATIVE_URL_ROOT?

I'm trying to precompile the assets but the paths will still turn out to be in the form of "/assets/.."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment