ruby-on-rails - Sprockets错误Sprockets::NotImplementedError:未实现自定义asset_path帮助器

标签 ruby-on-rails assets sprockets precompile rails-sprockets

尝试在本地预编译 Assets 时出现以下问题

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'

我一辈子都无法弄清楚为什么会这样-任何有关如何解决的建议将不胜感激!

更新

只有添加以下内容的初始化程序,我才能进行预编译:
Rails.application.assets.context_class.class_eval do
  def asset_path(path, options = {})
    return ''
  end
end

但是,如果这样做,当我推送到暂存环境时,来自Bootstrap的字形图标将具有空路径,因此不会呈现:
font-face{font-family:'Glyphicons Halflings';src:url("");src:url("") format("embedded-opentype"),url("") format("woff"),url("") format("truetype"),url("") 

更新2

如果我将初始化程序修改为以下内容,则会在预编译引导 Assets 中找到glypicons的路径,但它不会到达预编译的Glyphicon文件,而是未编译的 Assets 路径:
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")

这是我的gemfile.lock,它与 Assets /sprockets gem 有关:
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)

最佳答案

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

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

关于ruby-on-rails - Sprockets错误Sprockets::NotImplementedError:未实现自定义asset_path帮助器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947064/

相关文章:

未读取 Angular 6 scss 背景图像

ruby-on-rails - 为什么nginx找不到我的 Assets ?

ruby-on-rails - Caddy 不渲染 Assets

ios - 需要将 .MP4 文件从文档文件夹保存到照片库,但 Assets url 返回 nil

ruby-on-rails-3.2 - 强制 sprockets 在测试期间编译 Assets

ruby-on-rails - Rails 3.1 & Sprockets & 编译的 JS 文件

ruby-on-rails - 下拉选择数据库表

ruby-on-rails - 在 CentOS 中安装 ImagMagick 和 rmagick gem

ruby-on-rails - 如何从 Rails 中的 URL 查询解析多值字段

ruby-on-rails - 如何在我的 Rails 应用程序中测试 ActiveRecord::RecordNotFound?