Search code examples
ruby-on-railsruby-on-rails-4hamlnested-form-for

Accessing nested_form field values in rails


I am using nested forms and I would like to format the text_field using number_with_precision. However, I'm having a problem understanding how to hook into that value. It works in the parent form. As can see below, I have tried to use the thing object itself (which I thought would be the correct way). Where am I going wrong?

= bootstrap_nested_form_for @quote, label_errors: true, label_col: "col-sm-2", control_col: "col-sm-6" do |f|
  =f.text_field :primary_fee, value: (@quote.primary_fee > 0 ? number_with_precision(@quote.primary_fee, precision: 2) : "20.00"), prepend: "<i class='fa fa-dollar'></i>".html_safe, control_col: "col-md-6"

  = f.fields_for :things, :wrapper => false, label_errors: true, html: {class: 'form_inline'} do |thing|
    = thing.text_field :price, value: number_with_precision(thing.price, precision: 2), prepend: "<i class='fa fa-dollar'></i>".html_safe, label: "Cost", class: "input-sm"

Solution

  • thing in your case is a form object (containing whole bunch of data), so you should access the object of the thing, not thing itself.

    thing.object.price would work:

    = thing.text_field :price,
      input_html: { value: number_with_precision(thing.object.price, precision: 2) }