user483040
user483040

Reputation:

Rails 3.1 asset pipeline with SASS

I was reading this post about the Rails 3.1 asset pipeline and the author points something out that made me somewhat unhappy about moving to this new paradigm.

Because assets are compiled and during that step their names are changed to include the md5 hash of the content, you can no longer refer to these assets in your CSS files directly. Take this image for example, rails.png. It is renamed:

rails-9c0a079bdd7701d7e729bd956823d153.png

I prefer to use SASS for my CSS and specifically put image names into variables so that my CSS styles can be rapidly changed by altering those variables. Won't this be broken now because the names are replaced? Won't this also mess up more classic CSS image backgrounds as well?

If this is indeed an issue, how do we work around it? I'd prefer to keep using SASS. It makes extensive CSS files easier to manage.

Upvotes: 1

Views: 1491

Answers (2)

Victor Deryagin
Victor Deryagin

Reputation: 12215

In sass stylesheets you'll need to use image_path or image_url helper, provided by sass-rails to generate valid image paths. Another way is to embed erb into stylesheet, but it seems too messy to me.

Upvotes: 6

Falcon
Falcon

Reputation: 1463

Add .erb on the end of filename. And use it

.class { background-image: url(<%= asset_path 'image.png' %>) }

You can read about it here.

Upvotes: 0

Related Questions