Ruby on rails webpacker can't find images under asset_pack_path

后端 未结 5 1563
野的像风
野的像风 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:56

    1. Place your images in app/javascripts/images

    2. Put following code in application.js At TOP of the file.

      // top of packs/application.js

      const importAll = (r) => r.keys().map(r)

      importAll(require.context('../images', false, /\.(png|jpe?g|svg)$/));

    3. This will load all images from images folder and then it should be available in manifest for you to pick up using asset_pack_path

    4. In your erb files, use like <%= asset_pack_path 'media/images/image_name.png' %>

    -- Read this for more details https://github.com/rails/webpacker/issues/1839

提交回复
热议问题