Skip to content

Unable to use asset_path #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mgwidmann opened this issue Apr 24, 2017 · 8 comments
Closed

Unable to use asset_path #284

mgwidmann opened this issue Apr 24, 2017 · 8 comments

Comments

@mgwidmann
Copy link

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 usabilityhub/rails-erb-loader#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
@gauravtiwari
Copy link
Member

Hey, did you followed this guide - https://github.com/rails/webpacker/blob/master/README.md#linking-to-sprockets-assets in README?

@mgwidmann
Copy link
Author

Is there no way this can't just be included already as it is in a typical app/assets/javascripts/*.js.erb file?

@gauravtiwari
Copy link
Member

@mgwidmann At the moment no, it's in the Wishlist though 👍

@gauravtiwari
Copy link
Member

Documented the options in README - https://github.com/rails/webpacker#link-sprocket-assets

@lbrito1
Copy link
Contributor

lbrito1 commented Jun 12, 2018

@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

@gauravtiwari
Copy link
Member

@lbrito1 It's here now: https://github.com/rails/webpacker/blob/master/docs/assets.md under docs/ folder

@vmarquet
Copy link

vmarquet commented Feb 3, 2019

TLDR; use asset_pack_path instead of asset_path.

@fydelio
Copy link

fydelio commented Dec 22, 2020

in your your_file_name.js.erb file

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants