经过一番挖掘,我发现了这个问题。问题出现在Rails中,特别是Sprockets :: Helpers :: RailsHelper :: AssetPaths#compute_public_path。 Sprockets :: Helpers :: RailsHelper :: AssetPaths继承自ActionView :: AssetPaths并覆盖了很多方法。当通过Sass :: Rails :: Resolver调用compute_public_path时#public_path方法是sass-rails,rails sprocket helper可以完成解析资产的任务。 Sprockets :: Helpers :: RailsHelper :: AssetPaths#compute_public_path推迟超级,它是ActionView :: AssetPaths#compute_public_path。在这种方法中有has_request的条件?上rewrite_relative_url_root如下所示:
def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil)
...
source = rewrite_relative_url_root(source, relative_url_root) if has_request?
...
end
def relative_url_root
config = controller.config if controller.respond_to?(:config)
config ||= config.action_controller if config.action_controller.present?
config ||= config
config.relative_url_root
end
如果看一下rewrite_relative_url_root的内部它依赖于请求是存在和来自控制器变量推导它为了解决相对URL根的能力。问题是,当链轮解决这些资产的问题时,它没有控制器,因此没有要求。
上面的解决方案对我来说不适用于开发模式。下面是我现在使用的解决方案:
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
下面是一个相关的问题。不完全一样,但一个答案可以帮助你们两个:http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub -uri –
https://github.com/rails/rails/pull/2977它很快就会在rails中的Sprockets :: RailsHelper :: AssetPaths类中修复。 –
Google员工,请参阅我的回答http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri/12122877#12122877 –