question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to access Rails view helpers

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
rhys-vdwcommented, May 18, 2017

Fair enough. I think the ideal would be to see exactly what Rails does when compiling ERB and use the same code path.

This would require knowledge of this part of Rails’ code base.

I agree this would be nice to support. I am not personally planning to tackle this problem any time soon. I am, however, willing to give guidance and feedback about the approach taken.

I expect this is something that the folks over at rails/webpacker would be interested in having support to achieve parity with Sprockets.

1reaction
alassekcommented, Dec 9, 2017

I can’t speak for ERB or Erubi but Erubis provides #evaluate which performs an instance_eval against a context object you pass in.

https://github.com/kwatch/erubis/blob/3bad74be554caaa34527e7407138d4840a71c4f1/lib/erubis/evaluator.rb#L69-L75

    def evaluate(_context=Context.new)
      _context = Context.new(_context) if _context.is_a?(Hash)
      #return _context.instance_eval(@src, @filename || '(erubis)')
      #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)')
      @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)')
      return _context.instance_eval(&@_proc)
    end
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't access helper method in view - Stack Overflow
I get the error : undefined local variable or method `url_to_submit_event'. when i'm trying to access an helper method from a form in...
Read more >
Can't access Rails view helper methods #265 - nesquena/rabl
Rails View Helper methods don't work inside RABL templates: app/helpers/application_helper.rb: module ApplicationHelper def test "test" end ...
Read more >
Rails helpers: how to use them right - Alberto Almagro
In this post you will learn how to use Rails helpers right and what kind of code goes into apps/helpers coding some helpers...
Read more >
Action View Helpers - Ruby on Rails Guides
Action View HelpersAfter reading this guide, you will know: How to format dates, strings and numbers How to link to images, videos, stylesheets,...
Read more >
Rails View Helpers — Best Practices Guide | by Uduak Essien
Transitioning from HTML and CSS to Ruby curriculum to SQL and now Ruby on Rails has not just been amazing for me. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found