Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如何使用Rails以嵌套形式访问模型的虚拟属性_Ruby On Rails 3_Nested Forms_Virtual Attribute - Fatal编程技术网

Ruby on rails 3 如何使用Rails以嵌套形式访问模型的虚拟属性

Ruby on rails 3 如何使用Rails以嵌套形式访问模型的虚拟属性,ruby-on-rails-3,nested-forms,virtual-attribute,Ruby On Rails 3,Nested Forms,Virtual Attribute,我有一个基本的嵌套表单。我想访问嵌套表单模型的虚拟属性 Model 1: Lease Has_many :transactions accepts_nested_attributes_for :transactions, :reject_if => lambda { |a| a[:dated].blank? }, :allow_destroy => true ... Model 2: Transaction belongs_to :lease def balan

我有一个基本的嵌套表单。我想访问嵌套表单模型的虚拟属性

Model 1: Lease
  Has_many :transactions
  accepts_nested_attributes_for :transactions, :reject_if => lambda { |a| a[:dated].blank? }, :allow_destroy => true
  ...

Model 2: Transaction
  belongs_to :lease
  def balance_to_date(aDate)
    #Returns the current balance up to aDate
    ...
  end
  ...
在嵌套形式中,我想放置如下内容:

<td style="width:100px;"><%= nested_f.text_field :dated, size: 8 %> </td>
<td style="width:100px;"><%= nested_f.text_field :label, size: 8 %> </td>
<td style="width:100px;"><%= nested_f.text_field :credit, size: 6  %> </td>
<td style="width:100px;"><%= nested_f.text_field :debit, size: 6  %> </td>
<td style="width:100px;"><%= nested_f.balance_to_date(:dated) %> </td>
对于如何访问和显示模型的虚拟属性以及显示当前记录的日期,我们非常感谢。我希望我没有重复前面的问题

我使用的是Rails 3.2.12和Ruby 1.9.3

谢谢!
菲尔

如果我明白你想做什么,你就很接近了。只需向下钻取一个级别即可访问form builder正在使用的模型对象:

<td style="width:100px;"><%= nested_f.object.balance_to_date(:dated) %> </td>

就是这样!谢谢你,丹。我完全忘了。物体。
   Dated         Label      Credit    Debit   Balance  
 [ 1/1/2012 ]  [ Rent due ] [       ] [ 600 ]  -600  
 [ 1/2/2012 ]  [ Payment  ] [ 600   ] [     ]  0 
 [ 2/1/2012 ]  [ Rent due ] [       ] [ 600 ]  -600 
 [ 2/2/2012 ]  [ Payment  ] [ 500   ] [     ]  -100 
 [ 3/1/2012 ]  [ Rent due ] [       ] [ 600 ]  -700 
 [ 3/6/2012 ]  [ late fee ] [       ] [ 50  ]  -750 
 [ 3/7/2012 ]  [ Payment  ] [ 800   ] [     ]  50 
 [ 4/1/2012 ]  [ Rent due ] [       ] [ 600 ]  -550
<td style="width:100px;"><%= nested_f.object.balance_to_date(:dated) %> </td>