3

我一直在从2.3.11升级到Rails 3.1。要清楚的一个主要障碍是转换到资产管道。在这个过程中,我决定将我的CSS转换为sass(s​​css)。在rails 3.1中,通过管道传送的所有资源都会在生产中接收到附加到文件名的散列。因此,我的CSS中引用的所有图像现在需要在sass中使用图像路径或图像url助手。问题是,即使我在我的environment.rb文件中设置了ENV ['RAILS_RELATIVE_URL_ROOT'],但sass资产助手未能包含relative_url_root。Rails 3.1 SASS资产助手不包括RAILS_RELATIVE_URL_ROOT/relative_url_root

只是为了清楚起见,增加在轨3.1 relative_url_root,添加以下行到我的environment.rb文件中:

ENV['RAILS_RELATIVE_URL_ROOT'] = '/foo' 

和下面的行添加到我的虚拟主机:

RailsBaseURI /foo 

这种策略似乎适用于所有链接等。这只是sass中的资产助手,似乎不能正常工作。任何想法,将不胜感激。

+0

下面是一个相关的问题。不完全一样,但一个答案可以帮助你们两个:http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub -uri –

+0

https://github.com/rails/rails/pull/2977它很快就会在rails中的Sprockets :: RailsHelper :: AssetPaths类中修复。 –

+0

Google员工,请参阅我的回答http://stackoverflow.com/questions/7293918/broken-precompiled-assets-in-rails-3-1-when-deploying-to-a-sub-uri/12122877#12122877 –

回答

3

经过一番挖掘,我发现了这个问题。问题出现在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