在rails中访问nested_form字段值

时间:2015-11-29 19:13:01

标签: ruby-on-rails ruby-on-rails-4 haml nested-form-for

我正在使用嵌套表单,我想使用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"

1 个答案:

答案 0 :(得分:1)

在您的情况下,

thing是一个表单对象(包含整组数据),因此您应该访问thing的对象,而不是自己的对象。

thing.object.price可行:

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