简体   繁体   English

simple_form添加没有值的属性

[英]simple_form adding attributes with no value

I'm using simple_form under a Rails 3.2 app with AngularJS and haml. 我在带有AngularJS和haml的Rails 3.2应用程序下使用simple_form。

I've got this input field where we are storing a cost and I would like to use the angularjs directive 'form-cents-to-dollars' but the simple_form input_html options require a key with a value, this is just the key or attribute as AngularJS calls it. 我已经在此输入字段中存储了成本,我想使用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" }

The error I get when running this code is 'syntax error, unexpected }, expecting =>. 运行此代码时出现的错误是“语法错误,意外的},期望=>。

Is it possible to set just the 'key' using simple form? 是否可以使用简单的形式仅设置“键”? Or is there a way to send it to AngularJS as a key/value pair which would work with the attribute directive? 还是有一种方法可以将它作为可以与attribute指令一起使用的键/值对发送给AngularJS?

Somewhat hacky, but you can always use an empty string as your value. 有点骇人听闻,但您始终可以使用空字符串作为值。

 = 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 will drop the empty string and add form-cents-to-dollars as an attribute on the input element. Rails将丢弃空字符串,并在输入元素上添加form-cents-to-dollars作为属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM