访问 rails 中的 nested_form 字段值

Accessing nested_form field values in rails

我正在使用嵌套表单,我想使用 number_with_precision 格式化 text_field。但是,我在理解如何挂钩该值时遇到问题。它以父形式工作。如下所示,我尝试使用 thing 对象本身(我认为这是正确的方法)。我哪里错了?

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

thing 在你的例子中是一个表单对象(包含大量数据),所以你应该访问 thing 的对象,而不是事物本身。

thing.object.price 可以:

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