Sprockets错误Sprockets :: NotImplementedError:未实现自定义asset_path帮助程序

cma*_*n77 8 assets ruby-on-rails precompile sprockets rails-sprockets

尝试在本地预编译资产时,我得到以下内容

RAILS_ENV=production bundle exec rake assets:precompile

rake aborted!
Sprockets::NotImplementedError: Custom asset_path helper is not implemented

Extend your environment context with a custom method.

    environment.context_class.class_eval do
      def asset_path(path, options = {})
      end
    end
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/sprockets-3.6.0/lib/sprockets/context.rb:198:in `asset_path'
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/sprockets-3.6.0/lib/sprockets/context.rb:218:in `font_path'
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/font-awesome-rails-4.6.1.0/app/assets/stylesheets/font-awesome.css.erb:15:in `_evaluate_template'
Run Code Online (Sandbox Code Playgroud)

我不能为我的生活弄清楚为什么会这样 - 任何有关如何解决的建议将不胜感激!

更新

只有在我添加具有以下内容的初始化程序时,我才能进行预编译:

Rails.application.assets.context_class.class_eval do
  def asset_path(path, options = {})
    return ''
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,当我推送到我的暂存环境时,Bootstrap中的glyphicons有一个空路径,因此不会渲染:

font-face{font-family:'Glyphicons Halflings';src:url("");src:url("") format("embedded-opentype"),url("") format("woff"),url("") format("truetype"),url("") 
Run Code Online (Sandbox Code Playgroud)

更新2

如果我将初始化程序修改为如下所示,我在预编译bootstrap资产中获取了一个用于glypicons的路径,但它不是预编译的Glyphicon文件,而是非编译资产路径:

Rails.application.assets.context_class.class_eval do
  def asset_path(path, options = {})
    #return ''
    "/assets/#{path}"
  end
end

@font-face{font-family:'Glyphicons Halflings';src:url("/assets/bootstrap/glyphicons-halflings-regular.eot");src:url("/assets/bootstrap/glyphicons-halflings-regular.eot?#iefix") format("embedded-opentype"),url("/assets/bootstrap/glyphicons-halflings-regular.woff") format("woff"),url("/assets/bootstrap/glyphicons-halflings-regular.ttf") format("truetype"),url("/assets/bootstrap/glyphicons-halflings-regular.svg#glyphicons_halflingsregular")
Run Code Online (Sandbox Code Playgroud)

这是我的gemfile.lock,因为它与assets/sprockets gems有关:

bootstrap-sass (3.1.1.0)
      sass (~> 3.2)
rails (4.2.5.2)
      actionmailer (= 4.2.5.2)
      actionpack (= 4.2.5.2)
      actionview (= 4.2.5.2)
      activejob (= 4.2.5.2)
      activemodel (= 4.2.5.2)
      activerecord (= 4.2.5.2)
      activesupport (= 4.2.5.2)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.5.2)
      sprockets-rails
sass (3.2.19)
    sass-rails (4.0.5)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.2.2)
      sprockets (~> 2.8, < 3.0)
      sprockets-rails (~> 2.0)
Run Code Online (Sandbox Code Playgroud)

Rub*_*der 1

您可以轻松克服这个问题。尽管您当前的问题更多是与版本相关的问题,并且不确定您是否正确捆绑更新。但还有替代解决方案。尝试安装以下 gem:

https://github.com/petebrowne/sprockets-helpers