Search code examples
ruby-on-railswebpackwebpack-4ruby-on-rails-6

using(adding) images to html with help image_pack_tag by Webpacker in Rails 6


I have a problem with adding image to web-page I try to use Webpack so I download images in application.js

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

after that i try to implement their in html.erb

          <div class="modal-body"
          <%= image_pack_tag 'calibration.png' %>
          </div>

so, i have this error what i do wrong? and what i need to do? enter image description here


Solution

  • The problem is solved. My mistake was that I specified the wrong path; a correct variant is:

    const images = require.context('../../assets/images', true)
    

    After that, place the image in public/packs/media/images/ so you can implement it in HTML in two ways:

    1. <%= image_pack_tag 'media/images/name_of_image.png' %>
    2. <img src="<%= asset_pack_path 'media/images/name_of_image.png' %>" width="100%" height="100%" alt=" demo instructions"></img>