Rails Middleman 中的助手`没有将 Symbol 隐式转换为 String`

Rails helper in Middleman `no implicit conversion of Symbol into String`

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

no implicit conversion of Symbol into StringRuby /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

版本和配置导入文件:

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

它似乎在这一行失败了:

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

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

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