Rails 5.2 + Webpacker:渲染部分内部javascript

Tim*_*van 7 ruby-on-rails webpack webpacker

我有一个javascript文件(something_controller.js.erb;技术上是Stimulus,但我认为不重要)我想在其中包含部分内容作为后续附加的HTML.

使用Webpacker启用了对ERB的支持,但是调用<%= render partial: 'shared/condition' %>不起作用.它只是悄悄地无法生成.js文件并包含它.

此代码不起作用:

const html = `<%= ApplicationController.renderer.render partial: 'shared/condition' %>`
Run Code Online (Sandbox Code Playgroud)

但是,这不是渲染器.渲染错误,因为这有效:

const html = `<%= ApplicationController.renderer.render inline: 'something' %>`
Run Code Online (Sandbox Code Playgroud)

shared/_condition.html.erb的内容并不奇怪,并且没有变量:

<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>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了我能想到的路径的每个组合:app/views/shared/condition,/ app/views/shared/condition,带有_,带有.html.erb.我已经试过渲染template:file:...我难倒.

半相关:有什么地方我可以看到产生的任何错误?日志显示编译通常成功,但是没有生成简单的控制器.我找不到任何明显的错误日志.

ETA:在development.log中,出现:

[Webpacker] Compiling…
  Rendered shared/_condition.html.erb (36.1ms)
[Webpacker] Compiled all packs in /Users/timsullivan/dev/thing/public/packs
Run Code Online (Sandbox Code Playgroud)

...所以它似乎渲染部分,但something_controller.js文件未包含在组合的application.js中:

something_controller.js丢失了!

为了在某​​处找到错误,我尝试运行:

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
Run Code Online (Sandbox Code Playgroud)

Rya*_*ark 0

假设您使用 jquery 或其他东西将渲染的部分附加到元素,则需要转义 erb 标记的内容。

尝试这个:"<%= escape_javascript(render("/path/after/views/condition")) %>"

这里有更详尽的解释:https ://stackoverflow.com/a/1623813/695186