Rails image broken link

前端 未结 2 1736
不知归路
不知归路 2021-01-23 20:36

I\'m trying to link to an image in rails and its not working. I can link to assets/file.css as well as assets/file.js but assets/file.jpg doesnt work.

I tried a fresh ra

相关标签:
2条回答
  • 2021-01-23 20:52

    Ok so that's an expected behavior, you have to use Rails helpers:

     <%= image_tag "file.jpg" %>
    

    Your css files should be .erb. Within it, use:

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

    Reference is para 2.2.1 here.

    0 讨论(0)
  • 2021-01-23 20:56

    I just checked my code and my CSS looks like this:

    background-image:url(/assets/pinstripes.jpg);
    

    with the image uploaded to app/assets/images, of course. This looks like what you're using, but I can't tell for sure.

    0 讨论(0)
提交回复
热议问题