10

I am getting the following when trying to precompile my assets locally

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'

I can't for the life of me figure out why this is happening - any suggestions as how to tackle would be greatly appreciated!

Update

I am able to precompile only if I add an initializer with the following:

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

However, if I do so, when I push to my staging environment, the glyphicons from Bootstrap have an empty path and thus don't render:

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

UPDATE 2

If I modify the initializer to the below, I get a path in my precompiles bootstrap asset for glypicons, but it is not to the precompiled Glyphicon file, it's to the non compiled asset path:

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")

Here is my gemfile.lock as it relates to 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)
cman77
  • 1,753
  • 1
  • 22
  • 48
  • 1
    Can you share the full source for a minimal working example showing the problem? A git repo, eg. – smathy May 01 '16 at 21:07
  • Have you imported boostrap-sprockets as stated in the doc? https://github.com/twbs/bootstrap-sass#a-ruby-on-rails – Marc Lainez May 02 '16 at 05:42
  • I think the Glyphicon issue is just a symptom of my specifying a custom `asset_path` initializer. What I really need to figure out is why it's asking for that. If I create a brand new app it doesn't ask for that on precompile. – cman77 May 02 '16 at 15:29
  • can you show me where `asset_path` method is used in your source code?Also your gemfile please. – uday May 03 '16 at 18:49
  • Could you provide a few more lines of the stack trace? It seems to me a gem versioning issue (which I'm unable to reproduce). What also strikes me is that in the stacktrace you have `sprockets-3.6.0` version but a `< 3.0` version in your `Gemfile.lock`. I can see only a single sprocket version in my `gem list`. Perhaps you might want to try updating relevant gems one by one and see if the issue goes away with some gem update? – Matouš Borák May 04 '16 at 18:09

3 Answers3

2

I suggest, that the main problem is in sprockets version. in my rails 4.2.6 project i use sprockets 3.6.0, and in your Gemfile.lock snippet you use <3.0 version.

Just like an a test, you may update rails to 4.2.6 and try out fresh sprockets 3.6.0 + sprockets-rails 3.0.1 and fresh sass-rails 5.0.4 and sass 3.4.22 gem. I really do not know which one of this updates will help, but, I think it should work.

Mike Belyakov
  • 1,355
  • 1
  • 13
  • 24
  • I had actually tried that as well. I upgraded sprockets, sprockets-rails, sass-rails, sass all to the recent gems. – cman77 May 05 '16 at 14:56
2

I don't know if this qualifies as an answer or not, but I wound up just pulling down my production Heroku repo and confirmed everything precompiled. I then slowly upgraded all my gems and everything continued to precompile properly. I guess I'll never know what caused this. Thanks to those who contributed.

cman77
  • 1,753
  • 1
  • 22
  • 48
  • 1
    I went down a similar confusing road or removing and adding things but it turned out that the reason I was getting this error was because I had a `#include ActionView::Helpers::AssetUrlHelper` globally in an initializer :-/ – mraaroncruz Jun 01 '16 at 10:17
1

You can easily overcome this problem. Although your current problem is more a version related problem and not sure if you bundle update properly. But there is alternative solution. Try installing the following gem:

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

Rubyrider
  • 3,567
  • 1
  • 28
  • 34
  • I don't think adding another gem is the answer. As I stated I was able to pull down my prod repo and rebuild from there one by one without any need for the custom asset helper. – cman77 May 09 '16 at 13:21
  • I was able to reproduce the issue and adding the sprockets-helpers gem does resolve this issue. Any insight as to why I would all of a sudden need it? – cman77 May 24 '16 at 15:34