Question

I'm having an issue with Rails, Sprockets, SCSS and them not adding a fingerprint to the generated image files.

When I try to precompile my assets in an environment other than development, I get the following error:

rake aborted!
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

So I add the following to an initializer:

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

Which causes the error to go away, although I don't know if this is the right way about fixing the issue. It then produces a new error:

undefined method `include?' for nil:NilClass

However, if I add the following code:

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

It precompiles fine, but then the generated css does not have the fingerprint generated at the end of the url and therefore doesn't load when viewing the page.

The weird thing is that images that aren't added in a scss file, like in an img tag in the html, has the fingerprint appended fine and shows up when viewing the page.

Here is an example of some scss that doesn't load the fingerprint:

background: $blue url( asset-path('hero/hero-bg.jpg') ) repeat-x;

I'm running Rails 4.0.2. Here is my gemfile: https://gist.github.com/anthonycollini/8025540

Any help would be greatly appreciated. Thank you.

Was it helpful?

Solution

You shouldn't have to define asset_path since that is a helper that is already provided by Rails. It looks like you may have missed <%= %> when using asset_path in your scss file.

background: $blue url(<%= asset-path('hero/hero-bg.jpg') %>) repeat-x;

Also, don't forget to add the .erb file extension on your scss file like example.css.scss.erb.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top