[ACCEPTED]-sass-rails helpers "image-url", "asset-url" are not working in rails 3.2.1-asset-pipeline

Accepted answer
Score: 37

Despite what the documentation says, it 9 seems the default options in rails 3.2.6 8 allow you to just make things work with 7 even less path information in your CSS. E.g. ../app/assets/images/rails.png is 6 references in your example.css.scss file 5 with something like:

background: white url(rails.png) repeat-y;

You don't include the 4 image-url or asset-url into your scss (as far as I know), just 3 plain url(your_image.png). That bit of documentation appears 2 to be just an explanation of what it is 1 doing in the background.

Score: 11

When I hit this problem, it was because 10 I had not included the css file in the asset 9 pipeline for pre-compilation. As a result, it 8 would be generated at runtime. Because the 7 sass-rails gem is commonly in the :assets 6 group, the helpers are unavailable when 5 generating css files at runtime.

Try adding 4 the following line to your application.rb 3 (or production.rb):

config.assets.precompile += %w( public/omg.css )

I found the fix on this post including 2 a gotcha around naming the files when adding 1 them to the precompiler.

Score: 6

If you have updated your app to Rails 3.1 5 in the past, make sure you changed your 4 application.rb file from

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)


if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require *Rails.groups(:assets => %w(development test))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)

See this railscast on upgrading 3 to Rails 3.1 and adding the asset pipeline.

Update: Rails 2 4 goes back to the old way of doing it. Thanks 1 Aaron Gray!

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
Score: 4

Have you enabled the asset pipeline in application.rb?

config.assets.enabled = true

You 4 did right by setting the extension on your 3 Sass stylesheets to .css.scss. That lets Rails know 2 to parse the file with Sass first before 1 it emits the content as CSS.

Score: 1

You might want to try clearing /tmp/cache. I 8 am too new to Rails and Sass to know why 7 this worked, but it solved the same problem 6 for me after hours of searching.

BTW, this 5 worked despite the fact that I could see 4 other Sass directives, such as setting variables 3 and calculating with them, being executed. I'm 2 sure there is a very simple explanation, once 1 I have time to track it down.

Score: 1

I made the change suggested by @Ryan, as 2 well as upgrading sass-rails:

bundle update sass-rails

sass 3.2.6 1 worked for me, while 3.2.5 did not.

Score: 0

We just had the same problem and fixed it 4 by explicitly requiring sprockets in the 3 Gemfile (even though it's a dependency of 2 ActionPack):

group :assets do
  gem 'sprockets'
  gem 'sass-rails', '~> 3.2.3'
  # ...

I don't know why, but it works 1 now. ;-)

Score: 0

I've been banging my head against this for 9 days. The only solution that worked for 8 me was as follows:

  1. Make sure sass-rails to your :development group in your Gemfile.
  2. If that doesn't fix it, add 7 the following to a new file in config/initializers/ called 6 something like "horrible_sass_patch.rb":

      require 'sass-rails'
    if Class.const_defined? "Sass::Script::Functions"
      module Sass::Script::Functions
        # This function exists, but doesn't automatically register
        declare :asset_url, [:value]
        declare :image_url, [:value]
        declare :font_url, [:value]
        # ... etc

Note: This 5 requires that you are using the "active" Bundler 4 loading mechanism, i.e. your application.rb 3 uses the following:

Bundler.require *Rails.groups(:assets => %w(development test))

... and if your stylesheets 2 are in vendor, make sure they're included 1 in Sass's configuration:

if config.respond_to? :sass
  config.sass.load_paths << Rails.root.join('vendor', 'assets', 'stylesheets')

More Related questions