Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS in Rails Asset Path not processed by ERB in development

I have a Rails app with the following in /app/assets/stylesheets/styles.css.erb:

...
#nestedbg {
    background-position: left top;
    background-image: url(<%= asset_path 'siteheader2.png' %>);
    background-repeat: repeat-x;
    background-attachment: fixed;
}
...

When I run rake assets:precompile and then run rails s -e production, everything works as expected. However, when I remove the precompiled assets and run rails s in development, the CSS file comes up as shown above instead of being properly substituted.

I tried putting config.assets.compile = true in /config/environments/development.rb and that did not help.

Any ideas? Thanks.

like image 213
Moshe Katz Avatar asked Dec 09 '22 04:12

Moshe Katz


1 Answers

I honestly cannot say why this is not interpreted correctly in your case, but I have a much better workaround to offer: skip erb interpreting altogether.

You can do this like so:

/* styles.css.scss */
background-image:url(image_path("siteheader2.png"));

If you did not have a chance to I would also suggest to have a look at SASS: it is integrated in the Rails asset pipeline and lets you do cool things like variable declarations, nesting, mixins, ...

like image 187
emrass Avatar answered Jan 16 '23 22:01

emrass