我正在使用嵌套表单,我想使用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"
答案 0 :(得分:1)
thing
是一个表单对象(包含整组数据),因此您应该访问thing
的对象,而不是自己的对象。
thing.object.price
可行:
= thing.text_field :price,
input_html: { value: number_with_precision(thing.object.price, precision: 2) }