-
Notifications
You must be signed in to change notification settings - Fork 28
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
Unable to access Rails view helpers #33
Comments
Hey @mgwidmann, sorry for the slow response. I'm open to adding support for this. Is it possible to do something like this? // urls.js.erb
<% include Rails.application.routes.url_helpers %>
export const my_favourite_url = <%= my_favourite_url %> Or do these helpers need to be included before the template is rendered? If so then I think explicitly listing helpers at the top of a {
loader: 'rails-erb-loader',
options: {
runner: 'ruby',
engine: {
name: 'erubis',
viewContext: 'MyViewContext' // Name of symbol defined in Rails project
}
}
} Or... {
loader: 'rails-erb-loader',
options: {
runner: 'ruby',
engine: {
name: 'erubis',
includes: [
'ActionView::Helpers',
'Rails.application.routes.url_helpers'
]
}
}
} Ideally though we'd be able to hook directly into the Rails API that renders ERB. I haven't looked at this, but if it's an option then we could just set engine to |
I'd personally like to see the second. I think creating your own view context is too much work. |
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 |
I can't speak for ERB or Erubi but Erubis provides 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 |
Hi, I had a similar problem and solved it including the helper like this in the
|
With a
js.erb
file of the following contents:The above crashes with the following exception (application name replaced with
myapp
):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):The text was updated successfully, but these errors were encountered: