Ruby on rails webpacker can't find images under asset_pack_path

后端 未结 5 1570
野的像风
野的像风 2021-02-06 15:16

I have a problem with including images in my views using Ruby on Rails Webpacker. When i try to insert image in my view using html img tag and asset_pack_path i got an error. My

5条回答
  •  情话喂你
    2021-02-06 15:40

    It looks like the path is not in the manifest file.

    In Rails 6 there are two commented lines in application.js. Regardless of what rails version you're running just make sure you add them in:

    const images = require.context('../images', true)
    const imagePath = (name) => images(name, true)
    

    Alternatively, you can force the images to be added to the manifest by moving your folder inside packs, eg. packs/images. The path would change though to media/images/your-file.png.

    For seeing if the path exists or not, I found it easier to just lookup it up from ruby directly:

    Webpacker.manifest.send(:data).keys.grep /logo/
    

提交回复
热议问题