simple_form添加没有值的属性

simple_form adding attributes with no value

本文关键字:属性 form 添加 simple      更新时间:2023-09-26

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

我有一个存储成本的输入字段,我想使用angularjs指令"从美分到美元",但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,与属性指令一起使用?

有些古怪,但您可以始终使用空字符串作为值。

 = 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将删除空字符串,并将表单美分添加到美元中,作为输入元素上的属性。