I have a javascript file (something_controller.js.erb; technically Stimulus, but I don't think that matters) where I'd like to include the content of a partial as the HTML it'll append later on.
I enabled support for ERB with Webpacker, but a call to <%= render partial: 'shared/condition' %>
doesn’t work. It simply quietly fails to generate the .js file and include it.
This code doesn’t work:
const html = `<%= ApplicationController.renderer.render partial: 'shared/condition' %>`
It’s not a renderer.render error, though, because this works:
const html = `<%= ApplicationController.renderer.render inline: 'something' %>`
The contents of shared/_condition.html.erb isn’t weird, and has no variables:
<div data-controller='condition'>
<a href='#' data-action='condition#remove'><i class="fas fa-trash-alt"></i></a>
<a href='#' data-toggle="popover" data-target='condition.item' data-action='condition#doNothing'>Item</a>
<a href='#' data-toggle="popover" data-target='condition.value' data-action='condition#doNothing'>Value</a>
</div>
I’ve tried every combination of path I can think of: app/views/shared/condition, /app/views/shared/condition, with the _, with the .html.erb. I've tried rendering template:
and file:
... I'm stumped.
Semi-related: is there somewhere I can see any errors generated? The log shows the compilation succeeded in general, but the controller this is in simply isn’t generated. I can’t find any obvious error log.
ETA: In development.log, this appears:
[Webpacker] Compiling…
Rendered shared/_condition.html.erb (36.1ms)
[Webpacker] Compiled all packs in /Users/timsullivan/dev/thing/public/packs
... so it does seem to be rendering the partial, but the something_controller.js file is not being included in the combined application.js:
In an attempt to find an error somewhere, I tried running:
timsullivan$ rails assets:precompile
yarn install v1.6.0
(node:45691) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[1/4] Resolving packages...
success Already up-to-date.
✨ Done in 0.49s.
Webpacker is installed
Using /Users/timsullivan/dev/thing/config/webpacker.yml file for setting up webpack paths
Compiling…
Compiled all packs in /Users/timsullivan/dev/thing/public/packs