Not sure if this belongs here, but since this is part of the rails new --webpack output, I figure I'd at least make this project aware.
Copied from https://github.com/usabilityhub/rails-erb-loader/issues/33
With a js.erb file of the following contents:
const ASSET = "<%= asset_path('my-asset') %>";
The above crashes with the following exception (application name replaced with myapp):
ERROR in ./client/js/constants/AssetConstants.js.erb
Module build failed: Error: Command failed: DISABLE_SPRING=1 /Users/mattwidmann/code/ruby/myapp/bin/rails runner /Users/mattwidmann/code/ruby/myapp/node_modules/rails-erb-loader/erb_transformer.rb __RAILS_ERB_LOADER_DELIMETER__ erb
(erb):24:in `<main>': undefined method `asset_path' for main:Object (NoMethodError)
from /Users/mattwidmann/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/erb.rb:864:in `eval'
from /Users/mattwidmann/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/erb.rb:864:in `result'
from /Users/mattwidmann/code/ruby/myapp/node_modules/rails-erb-loader/erb_transformer.rb:19:in `<top (required)>'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/runner.rb:60:in `load'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/runner.rb:60:in `<top (required)>'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:123:in `require'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:123:in `require_command!'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:90:in `runner'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/mattwidmann/.rvm/gems/ruby-2.3.3@myapp/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/mattwidmann/code/ruby/myapp/bin/rails:9:in `require'
from /Users/mattwidmann/code/ruby/myap/bin/rails:9:in `<main>'
at ChildProcess.exithandler (child_process.js:211:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:885:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:501:12)
Same for things like javascript_path, image_path, ect.
I have got this to work with my own rake task separately, heres the code for doing that. The trick is being able to add include Rails.application.routes.url_helpers, which most templating engines do not make very easy (Erubis is terrible as you can see below):
# Copied from https://github.com/rails/rails/blob/master/actionview/test/template/erb/helper.rb
class ViewContext
include ActionView::Helpers
include Rails.application.routes.url_helpers
attr_accessor :output_buffer, :controller
def protect_against_forgery?() false end
end
filename = './client/js/constants/AssetConstants.js.erb'
result = ActionView::Template::Handlers::Erubis.new(File.read(filename)).evaluate(ViewContext.new)
# `result` is the contents of the file after its been evaluated
Hey, did you followed this guide - https://github.com/rails/webpacker/blob/master/README.md#linking-to-sprockets-assets in README?
Is there no way this can't just be included already as it is in a typical app/assets/javascripts/*.js.erb file?
@mgwidmann At the moment no, it's in the Wishlist though 馃憤
Documented the options in README - https://github.com/rails/webpacker#link-sprocket-assets
@gauravtiwari that specific part of the README seems to have been removed for some reason.
For anyone else interested, here's a point in git history where the relevant section was still included in the README: https://github.com/rails/webpacker/tree/65cc86067bf63f1578bbfe5e4016f8b144d297fd
@lbrito1 It's here now: https://github.com/rails/webpacker/blob/master/docs/assets.md under docs/ folder
TLDR; use asset_pack_path instead of asset_path.
Most helpful comment
TLDR; use
asset_pack_pathinstead ofasset_path.