在本地尝试预编译我的资产时,我遇到了以下问题
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的glyphicons路径为空,因此无法呈现:font-face{font-family:'Glyphicons Halflings';src:url("");src:url("") format("embedded-opentype"),url("") format("woff"),url("") format("truetype"),url("")
更新2
如果我将初始值修改为以下内容,我会在预编译的引导资产中获得一个路径,用于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")
这是与资产/ Sprockets 宝石有关的我的 gemfile.lock 文件:
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)
asset_path
初始化程序的症状。我真正需要弄清楚的是为什么它会要求这样做。如果我创建一个全新的应用程序,它不会在预编译时要求这样做。 - cman77asset_path
方法的使用吗?还有你的 Gemfile 文件。 - udaysprockets-3.6.0
版本,但在你的Gemfile.lock
中是< 3.0
版本。在我的gem list
中只能看到一个sprocket版本。也许你可以尝试逐个更新相关的gems,看看是否通过某些gem更新解决了这个问题? - Matouš Borák