【问题标题】:Accessing nested_form field values in rails访问 rails 中的nested_form 字段值
【发布时间】:2015-11-29 19:13:00
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    thing 在您的情况下是一个表单对象(包含一大堆数据),因此您应该访问thing 的对象,而不是事物本身。

    thing.object.price 可以工作:

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

    【讨论】:

    • 谢谢。我之前看到过'object'的引用,但我不认为'object'是字面的。我以为我看到的参考是通过一个连接表。
    • 是的,奇怪的东西,但现在你会记得 :)
    • @Beengie 如果解决了问题,请考虑接受答案
    • 我打算接受这个答案。你太快了,我不得不等待 5 分钟的计时器。大声笑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多