什么呢?body = 1在rails 3.1资产管道中做什么?

时间:2011-09-28 13:10:24

标签: ruby-on-rails-3.1 asset-pipeline

在开发过程中,我的所有javascript资源都附加了body=1 get变量。这到底是做什么的?

http://localhost:3000/assets/application.js?body=1

1 个答案:

答案 0 :(得分:32)

浏览Sprocket源代码我们发现:

  # Returns a 200 OK response tuple
  def ok_response(asset, env)
    if body_only?(env)
      [ 200, headers(env, asset, Rack::Utils.bytesize(asset.body)), [asset.body] ]
    else
      [ 200, headers(env, asset, asset.length), asset ]
    end
  end
body_only?

时设置

?body=1 or true

对于静态资产,Asset.body定义为:

def body
  # File is read everytime to avoid memory bloat of large binary files
  pathname.open('rb') { |f| f.read }
end

而将资产归还其自身则是“具有机架功能的身体对象”

# Add enumerator to allow `Asset` instances to be used as Rack
# compatible body objects.
def each
  yield to_s
end

当我们查看bundled_asset时,Asset.body被重新定义为仅检索资产的主体而不包括任何依赖项。 Asset.to_a被定义为在将数据传递给Rack时检索资产及其所有依赖项。

通过这种方式,资产不会组合在一起,而是作为单独的对象,因此单个CSS文件仍然是个人的。