gpt4 book ai didi

javascript - 在 ruby​​ on Rails 中使用模板文字

转载 作者:行者123 更新时间:2023-12-03 04:26:55 25 4
gpt4 key购买 nike

我有一个 ruby​​ on Rails 应用程序,它有一个使用 JavaScript 注入(inject)新表单字段的表单。注入(inject)的表单字段位于模板文字中。模板在这里:

    $("#formSet").append(`
<div class="row itemGroup">
<div class="form-group col">
<div class="input-group">
<input type="number" min="0" value="1" name="quotation[items_attributes][${count}][qty]" id="item-qty-${count}" class="form-control qty">
</div>
</div>
<div class="form-group col-5">
<div class="input-group">
<input type="text" name="quotation[items_attributes][${count}][description]" id="item-description-${count}" class="form-control desc">
</div>
</div>
<div class="form-group col">
<div class="input-group">
<input type="number" value="0" name="quotation[items_attributes][${count}][price]" step="any" min="0" id="item-price-${count}" class="form-control price">
</div>
</div>
<div class="form-group col">
<div class="input-group">
<input type="checkbox" name="quotation[items_attributes][${count}][tax]" value="1" id="item-tax-${count}" class="form-control tax" checked="checked">
</div>
</div>
<div class="form-group col">
<div class="input-group">
<input type="number" value="0.00" class="form-control subtotal" name="[items_attributes][${count}][subtotal]" readonly>
</div>
</div>
</div>
`);

但是,当我尝试预编译我的 Assets 以进行生产时,我得到:

ExecJS::RuntimeError: SyntaxError: Unexpected character '`'

有没有办法将 ES6 模板文字与 Rails 预编译器一起使用,或者有没有办法可以在不使用模板文字的情况下附加项目?

最佳答案

目前 Rails 默认配置不足以使用 ES6。您可以通过多种方法来设置项目以使用 ES6,即使不幸的是,并没有真正的“标准”方法来执行此操作。

选项 1:如果您的 Rails 版本是 4.2 且 Sprockets 3,那么您可以使用 https://github.com/rmacklin/sprockets-bumble_d添加 ES6 支持。

选项 2:将 Sprockets 升级到版本 4,然后使用 https://github.com/fnando/babel-schmooze-sprocketshttps://github.com/babel/ruby-babel-transpiler添加 babel 以支持 ES6。

选项 3:如果您使用的是 Rails 5.1,则使用 webpack(代替 sprocket 或与 sprocket 一起使用)。 Rails 5.1 通过 webpacker gem 引入原生 webpack(以及 babel)支持。对于现有应用程序来说,这可能是更困难的选择。这里有一篇关于它的好文章:https://medium.com/statuscode/introducing-webpacker-7136d66cddfb#.cb4sixyah希望对您有帮助。

关于javascript - 在 ruby​​ on Rails 中使用模板文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com