Rails 3.1 SASS资产助手不包括RAILS_RELATIVE_URL_ROOT/relative_url_root [英] Rails 3.1 SASS asset helpers not not including RAILS_RELATIVE_URL_ROOT / relative_url_root

查看:59
本文介绍了Rails 3.1 SASS资产助手不包括RAILS_RELATIVE_URL_ROOT/relative_url_root的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力从2.3.11升级到Rails 3.1.要清除的主要障碍之一是向资产管道的转换.在此过程中,我决定将css转换为sass(s​​css).在rails 3.1中,通过管道交付的所有资产都将在生产中的文件名后面附加一个哈希.因此,我的CSS中引用的所有图像现在都需要在sass中使用image-path或image-url帮助器.问题是,即使我在我的environment.rb文件sass资产助手中设置了ENV ['RAILS_RELATIVE_URL_ROOT'],也无法包含relative_url_root.

I have been working on an upgrade to Rails 3.1 from 2.3.11. One of the main hurdles to clear is the conversion to the asset pipeline. In this process I decided to convert my css to sass (scss). In rails 3.1 all assets delivered through the pipeline receive a hash appended to the filename in production. Because of this, all the images referenced in my css now needed to use the image-path or image-url helpers in sass. The issue is, that even though I have set my ENV['RAILS_RELATIVE_URL_ROOT'] in my environment.rb file sass asset helper fail to include the relative_url_root.

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

Just for clarity, to add the relative_url_root in rails 3.1, I added the following line to my environment.rb file:

ENV['RAILS_RELATIVE_URL_ROOT'] = '/foo'

并将以下行添加到我的虚拟主机:

and add the following line to my virtual host:

RailsBaseURI /foo

此策略似乎适用于所有链接等.只是资产保护程序中的资产助手似乎无法正常工作.任何想法将不胜感激.

This strategy seems to work fine for all links and such. It is just the asset helpers in sass that don't seem to work properly. Any ideas would be appreciated.

推荐答案

经过一番挖掘,我发现了问题所在.问题出在Rails中,特别是Sprockets :: Helpers :: RailsHelper :: AssetPaths#compute_public_path. Sprockets :: Helpers :: RailsHelper :: AssetPaths继承自ActionView :: AssetPaths并重写了许多方法.通过Sass :: Rails :: Resolver#public_path方法调用sass-rails时,rails链轮辅助程序将承担解析资产的任务. Sprockets :: Helpers :: RailsHelper :: AssetPaths#compute_public_path遵循超级,即ActionView :: AssetPaths#compute_public_path.在此方法中,有一个has_request条件?在rewrite_relative_url_root上,如下所示:

After a bit of digging around, I have found the issue. The issue is in Rails, specifically Sprockets::Helpers::RailsHelper::AssetPaths#compute_public_path. Sprockets::Helpers::RailsHelper::AssetPaths inherits from ActionView::AssetPaths and overrides a number of methods. When compute_public_path is called through the Sass::Rails::Resolver#public_path method is sass-rails, the rails sprocket helper picks up the task of resolving the asset. Sprockets::Helpers::RailsHelper::AssetPaths#compute_public_path defers to super which is ActionView::AssetPaths#compute_public_path. In this method there is a condition of has_request? on rewrite_relative_url_root as seen below:

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根.问题在于,当链轮将这些资产解析为无用资产时,它就没有控制器,因此也就没有请求.

If you look at the internals of rewrite_relative_url_root it relies on a request to be present and the ability to derive it from the controller variable in order to resolve the relative url root. The issue is that when sprockets resolves these assets for sass it does not have a controller present and therefore no request.

以上解决方案对我而言不适用于开发模式.这是我目前使之使用的解决方案:

The solution above didn't work in development mode for me. Here is the solution that I am using to make it work for now:

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

这篇关于Rails 3.1 SASS资产助手不包括RAILS_RELATIVE_URL_ROOT/relative_url_root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆