What is the default path/location for an image in the jumpstart template if trying to access it from a Javascript function?
Trying to swap a play button image in a view for a pause button image, using innerHTML from a click event. The function is working as hoped, accept the image cant be found. I've tried a bunch of variations on the src path but I don't really understand enough about webpack/rails yet to nail it down.
What would be the correct path to use? Or is there I different I approach I should take?
playerIconEl.innerHTML = "<img src='/packs/media/images/pause.svg'>"
What would be the correct path to use? Or is there I different I approach I should take?
pause.svg
image in your app/assets/images/
folder instead? Looks like there are some svgs in there that you can see how their paths are referenced. For example, the logo.svg
file is rendered like <%= render_svg 'logo' ... %>
Although, I guess you would have to render the erb in the javascript. Not sure if that will work.
Thanks, Jake.
I've only been able to access
Interestingly,
In my view:
and in the JS
They both end up pointing to the same src folder so it seems like I'm in the right spot. I suspect there's some obvious syntax or caching type of issue that I'm overlooking.
A little time away will probably make things clearer. Thanks for chiming in.
I've only been able to access
app/assets/images/
using an asset_pack_path
helper as instructed by these webpacker docs and as you suspect, rendering erb in js is a non-starter.Interestingly,
In my view:
<img src="<%= asset_pack_path 'media/images/logo_blue_500_325.png' %>" />
generates<img src="/packs/media/images/logo.png">
and in the JS
playerIconEl.innerHTML = "<img src='/packs/media/images/pause.svg'>"
produces <img src="/packs/media/images/pause.svg">
They both end up pointing to the same src folder so it seems like I'm in the right spot. I suspect there's some obvious syntax or caching type of issue that I'm overlooking.
A little time away will probably make things clearer. Thanks for chiming in.
If you've got an SVG, outputting the actual content works great because you don't have to make another request in the browser. Runs a bit faster.
Otherwise, you'll have to actually use a data attribute or something to pass the icon URL to the JS because production assets will have a hash at the end of the filename. That will require you to always use webpacker's url helper to generate the url so you get the correct hash for the image in the filename.
Otherwise, you'll have to actually use a data attribute or something to pass the icon URL to the JS because production assets will have a hash at the end of the filename. That will require you to always use webpacker's url helper to generate the url so you get the correct hash for the image in the filename.
Notifications
You’re not receiving notifications from this thread.