I’ve configured webpacker to be able to process ERBs (bundle exec rails webpacker:install:erb
) because I need paths generated by Sprockets' asset_path
at build time so that I get properly generated asset URLs.
The ERB integration works with the simple example that you get after installing ERB/Webpacker integration, but I’m wondering how to have access to asset_path
. From what I’ve found so far, it doesn’t appear to be possible, https://github.com/rails/webpacker/issues/284.
Does anyone know if it's possible? In the meantime, I will keep digging 😉
Top comments (6)
What about this? github.com/rails/webpacker/blob/ma...
You beat me to it 😉I was about to comment it but refreshed first and saw your comment.
I tried this earlier today and it did not seem to work. Maybe I had something misconfigured. Will give it another go. Thanks @rhymes !
Oh snap! It works now. Must have been a case of not enough coffee. Thanks peeps!
May anyone who lands on this thread during a future search rejoice!
@rhymes , @ben , I figured out why it wasn't working the other day because it wasn't working for me today either. This is probably obvious to seasoned Rails devs, but this wasn't obvious to a Rails newb like me. 🙃
This is the sample code from the webpacker docs
I didn't realize that Sprockets checks to see if the image actually exists, i.e.
'rails.png'
. This is why I was getting exit code 1 from the rails-erb-loader.I discovered this by reading through one of the issues about exit code 1 for rails-erb-loader.
I followed the instructions from n-rodriguez to test it outside of the rails-erb-loader, so I ran
and sure enough, I got an error but with more details.
Sprockets::Rails::Helper::AssetNotFound
.So TIL that Sprockets actually looks for the asset. It just isn't trying to create a URL for any random image.
Remember folks, always dig when things aren't working 😉