【问题标题】:simple_form adding attributes with no valuesimple_form 添加没有值的属性
【发布时间】:2015-05-20 17:37:42
【问题描述】:

我在带有 AngularJS 和 haml 的 Rails 3.2 应用程序下使用 simple_form。

我有这个输入字段,我们在其中存储成本,我想使用 angularjs 指令“form-cents-to-dollars”,但 simple_form input_html 选项需要一个带值的键,这只是AngularJS 调用它的键或属性。

      = f.input :cost_per_unit_cents,
      input_html: {class: 'money',
      "ng-model" => "timesheet.cost_per_unit_cents",
      "ng-init" => "timesheet.cost_per_unit_cents=#{@timesheet.cost_per_unit_cents}",
      "ng-change" => "line_item_service.calc_total()",
      "form-cents-to-dollars" }

我在运行此代码时遇到的错误是“语法错误,意外 },期待 =>。

是否可以使用简单的形式只设置“键”?或者有没有办法将它作为键/值对发送到 AngularJS,这将与属性指令一起使用?

【问题讨论】:

    标签: javascript ruby-on-rails angularjs simple-form


    【解决方案1】:

    有点老套,但您始终可以使用空字符串作为您的值。

     = f.input :cost_per_unit_cents,
          input_html: {class: 'money',
          "ng-model" => "timesheet.cost_per_unit_cents",
          "ng-init" => "timesheet.cost_per_unit_cents=#{@timesheet.cost_per_unit_cents}",
          "ng-change" => "line_item_service.calc_total()",
          "form-cents-to-dollars" => "" }
    

    Rails 将删除空字符串并添加 form-cents-to-dollars 作为输入元素的属性。

    【讨论】:

    • 一点也不 hacky,一个密钥应该有一些价值,甚至为零!
    猜你喜欢
    • 2017-02-04
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多