DEV Community

Accessing asset_path in webpacker *.js.erb pack

Nick Taylor on February 18, 2020

I’ve configured webpacker to be able to process ERBs (bundle exec rails webpacker:install:erb) because I need paths generated by Sprockets' asset_p...
Collapse
 
rhymes profile image
rhymes
Collapse
 
glennmen profile image
Glenn Carremans

You beat me to it 😉I was about to comment it but refreshed first and saw your comment.

Collapse
 
nickytonline profile image
Nick Taylor

I tried this earlier today and it did not seem to work. Maybe I had something misconfigured. Will give it another go. Thanks @rhymes !

Collapse
 
nickytonline profile image
Nick Taylor

Oh snap! It works now. Must have been a case of not enough coffee. Thanks peeps!

Man about to drink a pot of coffee

Thread Thread
 
ben profile image
Ben Halpern

coffee

May anyone who lands on this thread during a future search rejoice!

Thread Thread
 
nickytonline profile image
Nick Taylor • Edited

@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

<% helpers = ActionController::Base.helpers %>
const railsImagePath = "<%= helpers.image_path('rails.png') %>"

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

cat app/javascript/packs/homePageFeed.jsx.erb  | bin/rails runner ./node_modules/rails-erb-loader/erb_transformer.rb __RAILS_ERB_LOADER_DELIMETER__ erb

and sure enough, I got an error but with more details.

Traceback (most recent call last):
        25: from bin/rails:4:in `<main>'
        24: from /Users/yolo/.gem/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
        23: from /Users/yolo/.gem/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
        22: from /Users/yolo/.gem/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
        21: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        20: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
        19: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
        18: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
        17: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
        16: from /Users/yolo/.gem/gems/railties-5.2.4.1/lib/rails/commands.rb:18:in `<main>'
        15: from /Users/yolo/.gem/gems/railties-5.2.4.1/lib/rails/command.rb:46:in `invoke'
        14: from /Users/yolo/.gem/gems/railties-5.2.4.1/lib/rails/command/base.rb:69:in `perform'
        13: from /Users/yolo/.gem/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
        12: from /Users/yolo/.gem/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
        11: from /Users/yolo/.gem/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
        10: from /Users/yolo/.gem/gems/railties-5.2.4.1/lib/rails/commands/runner/runner_command.rb:38:in `perform'
         9: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
         8: from /Users/yolo/.gem/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load'
         7: from /Users/yolo/dev/dev.to/node_modules/rails-erb-loader/erb_transformer.rb:19:in `<main>'
         6: from /Users/yolo/.rbenv/versions/2.6.5/lib/ruby/2.6.0/erb.rb:901:in `result'
         5: from /Users/yolo/.rbenv/versions/2.6.5/lib/ruby/2.6.0/erb.rb:901:in `eval'
         4: from (erb):8:in `<main>'
         3: from /Users/yolo/.gem/gems/actionview-5.2.4.1/lib/action_view/helpers/asset_url_helper.rb:375:in `image_path'
         2: from /Users/yolo/.gem/gems/cloudinary-1.13.2/lib/cloudinary/helper.rb:378:in `path_to_asset'
         1: from /Users/yolo/.gem/gems/actionview-5.2.4.1/lib/action_view/helpers/asset_url_helper.rb:200:in `asset_path'
/Users/yolo/.gem/gems/sprockets-rails-3.2.1/lib/sprockets/rails/helper.rb:83:in `compute_asset_path': The asset "rails.png" is not present in the asset pipeline. (Sprockets::Rails::Helper::AssetNotFound)

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 😉