0

我正在将 Gulp Starter Rails 助手移植到 Middleman,但出现以下错误:

no implicit conversion of Symbol into String相关Ruby /middleman-gulp-starter/helpers/gulp_asset_helper.rb: in join, line 14

我不确定 Rails 和 Middleman 之间的区别,以了解为什么这不起作用。

module GulpAssetHelper
  def gulp_asset_path(path, type = nil)
    rev_manifest = nil

    # In development, check for the manifest every time
    if !config[:build]
      rev_manifest = JSON.parse(File.read(REV_MANIFEST_PATH)) if File.exist?(REV_MANIFEST_PATH)
    # In production, use the manifest cached in initializers/gulp.rb
    else
      rev_manifest = REV_MANIFEST if defined?(REV_MANIFEST)
    end

    root = GULP_CONFIG['root']['dest'].gsub(/(.*)build\//, '/')
    asset_path = type ? File.join(GULP_CONFIG['tasks'][type]['dest'], path) : path # LINE 14
    asset_path = rev_manifest[asset_path] if rev_manifest
    asset_path = File.join(root, asset_path)
    File.absolute_path(asset_path, '/')
  end

  def gulp_js_path(path)
    gulp_asset_path(path, 'js')
    GULP_CONFIG
  end

  def gulp_css_path(path)
    gulp_asset_path(path, 'css')
  end

  def gulp_image_path(path)
    gulp_asset_path(path, 'images')
  end

  def sprite(id, classes = "", viewBox = "0 0 24 24")
    "<svg class='sprite -#{id} #{classes}' aria-hidden='true' preserveAspectRatio viewBox='#{viewBox}'><use xlink:href='#{gulp_image_path('sprites.svg')}##{id}' /></use></svg>".html_safe
  end
end

rev 和 config 导入文件:

GULP_CONFIG = JSON.parse(File.read('gulpfile.js/config.json'))
REV_MANIFEST_PATH = File.join(GULP_CONFIG['root']['dest'], 'rev-manifest.json')

if File.exist?(REV_MANIFEST_PATH)
  REV_MANIFEST = JSON.parse(File.read(REV_MANIFEST_PATH))
end

示例rev-manifest.json文件:

{
  "images/middleman-logo.svg": "images/middleman-logo-2e3d8b5ad1.svg",
  "javascripts/all.js": "javascripts/all-92681c51e741e0e1370c.js",
  "stylesheets/site.css": "stylesheets/site-9b25f1d1ac.css"
}

我已经输出了 gulpfile 的内容,所以知道它正在被正确读取。

你可以在这里找到完整的仓库:https ://github.com/craigmdennis/middleman-gulp-starter/tree/4_asset-helpers

4

1 回答 1

1

看起来它在这条线上失败了:

 asset_path = type ? File.join(GULP_CONFIG['tasks'][type]['dest'], path) : path

File.join方法需要字符串,因此GULP_CONFIG['tasks'][type]['dest']orpath不是字符串而是符号。尝试这样的事情:

asset_path = type ? File.join(GULP_CONFIG['tasks'][type.to_s]['dest'].to_s, path.to_s) : path.to_s
于 2016-04-06T01:08:48.440 回答