我有一个javascript文件(something_controller.js.erb;从技术上讲是Stimulus,但我认为这并不重要),我想在其中将部分内容作为HTML包含,稍后再附加。 / p>
我enabled support for ERB with Webpacker,但无法拨打电话<%= render partial: 'shared/condition' %>
。它只是简单地无法生成.js文件并包含它。
此代码无效:
const html = `<%= ApplicationController.renderer.render partial: 'shared/condition' %>`
这不是renderer.render错误,因为它可以起作用:
const html = `<%= ApplicationController.renderer.render inline: 'something' %>`
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>
我尝试了所有可以想到的路径组合: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
...因此它似乎确实正在渲染部分图像,但是something_controller.js文件未包含在组合的application.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
答案 0 :(得分:0)
假设您要使用jquery或其他内容将呈现的部分附加到元素上,则需要转义erb标签的内容。
尝试以下操作:"<%= escape_javascript(render("/path/after/views/condition")) %>"
此处有更详尽的解释:docs on that found here