【问题标题】:ruby injections in coffeescript execute despite conditions尽管有条件,coffeescript 中的红宝石注入仍会执行
【发布时间】:2019-11-29 04:57:08
【问题描述】:

在 RoR 项目中 -> 视图 -> ... -> create.js.coffee 代码

$("<%= j(render partial: 'invoice_equipment/invoice_equipment', object: @invoice_equipment) %>").appendTo("#invoice_equipment_list")

总是执行,不依赖于之前的条件。

在这个文件中,我想为用户显示一些消息,如果他尝试创建记录,那么数据库中已经存在。

如果从文件中删除该行,另一个js逐行正常工作。

所有文件:

if "<% @invoice_equipment %>"
  $("<%= j(render partial: 'invoice_equipment/invoice_equipment', object: @invoice_equipment) %>").appendTo("#invoice_equipment_list")
  M.toast({html: "<%= I18n.t('invoice_equipment.added') %>", displayLength: 3000, classes: 'rounded'})
else
  M.toast({html: "<%= I18n.t('invoice_equipment.already_added') %>", displayLength: 3000, classes: 'rounded'})

$('#new_invoice_equipment')[0].reset()
$('#invoice_equipment_equipment').focus()

@invoice_equipment 可以是 nil 或 ActiveRecord。但是在这两种情况下,如果 @invoice_equipment 为 nil,RoR 都会尝试渲染部分并下降。

所以,我认为,在创建 js 文件之前执行注入的 ruby​​ 代码。

我是对的?

【问题讨论】:

    标签: ruby coffeescript


    【解决方案1】:

    这可能是因为您在条件条件中混合使用了 javascript 和 rails 模板。尝试重新格式化您的条件,使其不使用 javascript if:

    <% if @invoice_equipment.present? %>
      # Your code to render when the object is present
    <% else %>
      # Your code to render when the object is missing
    <% end %>
    
    

    【讨论】:

    • Tnx 为您的回复,但这不起作用。 CoffeeScript 无法编译为 js 和 rails 显示错误:ActionView::Template::Error (SyntaxError: [stdin]:3:1: missing indentation)
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2011-08-10
    相关资源
    最近更新 更多