Are we supposed to use something else aside from image-url
and others in Rails 4? They return different values that don\'t seem to make sense. If I have l
I had a similar problem, trying to add a background image with inline css. No need to specify the images folder due to the way asset sync works.
This worked for me:
background-image: url('/assets/image.jpg');
I just found out, that by using asset_url
helper you solve that problem.
asset_url("backgrounds/pattern.png", image)
Your first formulation, image_url('logo.png')
, is correct. If the image is found, it will generate the path /assets/logo.png
(plus a hash in production). However, if Rails cannot find the image that you named, it will fall back to /images/logo.png
.
The next question is: why isn't Rails finding your image? If you put it in app/assets/images/logo.png, then you should be able to access it by going to http://localhost:3000/assets/logo.png
.
If that works, but your CSS isn't updating, you may need to clear the cache. Delete tmp/cache/assets
from your project directory and restart the server (webrick, etc.).
If that fails, you can also try just using background-image: url(logo.png);
That will cause your CSS to look for files with the same relative path (which in this case is /assets).
for stylesheets: url(asset_path('image.jpg'))
I just had this issue myself. 3 points that will hopefully help:
app/assets/images
directory, then you should be able to call the image directly with no prefix in the path. ie. image_url('logo.png')
background-image:
property, then your line of code should be background-image: image-url('logo.png')
. This works for both less and sass stylesheets. If you are using it inline in the view, then you will need to use the built in image_tag
helper in rails to output your image. Once again, no prefixing <%= image_tag 'logo.png' %>
rake assets:precompile
to generate your assets, or rake assets:precompile RAILS_ENV=production
for production, otherwise, your production environment will not have the fingerprinted assets when loading the page.Also for those commands in point 3 you will need to prefix them with bundle exec
if you are running bundler.
Rails 4.0.0 will look image defined with image-url
in same directory structure with your css file.
For example, if your css in assets/stylesheets/main.css.scss
, image-url('logo.png')
becomes url(/assets/logo.png)
.
If you move your css file to assets/stylesheets/cpanel/main.css.scss
, image-url('logo.png')
becomes /assets/cpanel/logo.png
.
If you want to use image directly under assets/images directory, you can use asset-url('logo.png')