簡體   English   中英

在Rails中的ruby中使用模板文字

[英]Using template literals in ruby on rails

我有一個Ruby on Rails應用程序,該應用程序具有使用JavaScript注入新表單字段的表單。 注入的表單字段在模板文字中。 模板在這里:

    $("#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>
`);

但是,當我嘗試對資產進行預編譯以進行生產時,會得到:

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

是否可以在rails預編譯器中使用ES6模板文字,或者是否可以不使用模板文字來追加項目?

目前,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(代替鏈輪或與鏈輪一起使用)。 Rails 5.1通過webpacker gem引入了本機webpack(從而實現了babel)支持。 對於現有應用程序,這可能是更困難的選擇。 這里有一篇很好的文章: https : //medium.com/statuscode/introducing-webpacker-7136d66cddfb#.cb4sixyah希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM