问题
I am having trouble trying to rake assets:precompile
in my rails 3.1 app. I keep getting the following error:
rake aborted!
Invalid CSS after "...und-image: url(": expected ")", was "<%= asset_path(..."
It seems that the erb preprocessor is not being invokeb but my file is called style.css.scss.erb
. Any suggestions?
回答1:
Ruby documentation seems a bit unclear on a few things such as the usage of the asset_path
and other such helper in stylesheets. Anyways this is what I did to get around the exact same problem:
- I decided to do this the SASS-way by changing my stylesheet extensions from css to scss.
-
The image references in my code were changed from
background-image: url(<%= asset_path 'blah.png' %>);
tobackground-image: image-url("blah.png");
I've also added the config.assets.digest = true
line to my config/appliction.rb
file because that seemed to get my output HTML to refer to the hashed filenames. Without the digest
flag set to true I get all of my link tags starting off with <link href="/assets/print.css?body=1" ...
or <href="/assets/favicon.png"...
which pretty much defies the purpose of using the assets pipeline. Especially the favicon file will still be cached by the servers and CDN's along the way.
Explicitely setting the digest flag to true gets me <link href="/assets/print-e47f5a48af04ce6854c840d74cd28fba.css?body=1"
and <link href="/assets/favicon-15fb5e00d868940bc32db7996e10f594.png" ...
回答2:
Change the file extension from
xxx.scss.css
to xxx.scss.css.erb and everything shoule be fine
回答3:
Even though an answer has already been accepted, and my specific solution may not have solved the OP's issue, this question was the top google hit so thought this might help someone else. I couldn't accept the idea of having to change all my stylesheets to use the SASS-style asset paths instead of ERB-style, because ERB should work. After some digging, I realized that I had so many files with embedded ruby asset_path helpers, and I had missed one in the app/assets/stylesheets directory that still had just a .css extension (forgot to add .erb). Also, I was including vendor.css, and that included one other file in vendor/assets/stylesheets that needed the .erb. Finally, I was using an older version of svn which still used the .svn directories at every level of the hierarchy, and since I had a 'require_tree .' in application.css, the .svn-base files may have been getting compiled as well, and obviously wouldn't be run through the erb processor. Fixing all the above got me working again.
回答4:
This is a sass-rails
error, as discussed here.
I had the same question and found out that the solution is by installing sass-rais-path.
This gets Rails to work SASS + ERB as expected, even though you may continue using the asset_path
helper.
来源:https://stackoverflow.com/questions/7693226/assets-not-being-run-though-the-erb-preprocessor