Search code examples
javascriptruby-on-railsangularjssimple-form

simple_form adding attributes with no value


I'm using simple_form under a Rails 3.2 app with AngularJS and haml.

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.

      = 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?


Solution

  • 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.