[英]How to access virtual attributes of model in a nested form with Rails
我有一個基本的嵌套表單。 我想訪問嵌套表單模型的虛擬屬性。
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>
我希望以下內容能使我保持平衡。
nested_f.balance_to_date(:日期)
或者能夠做類似的事情
運行如下所示的代碼給我:
undefined method `balance_to_date' for#<ActionView::Helpers::FormBuilder:0xac78bac>
除了虛擬屬性錯誤外,此表單還可以正常工作。
該代碼應生成一個可編輯的交易表,余額到那時為止。 ([xx]是我顯示輸入字段的方式)。
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
任何有關如何訪問和顯示模型的虛擬屬性以及來自當前記錄的日期的建議都將不勝感激。 我希望我沒有重復上一個問題。
我正在使用Rails 3.2.12和Ruby 1.9.3。
謝謝! 菲爾
如果我了解您要執行的操作,那么您將非常接近。 您只需要向下鑽取一個級別即可訪問表單構建器正在使用的模型對象:
<td style="width:100px;"><%= nested_f.object.balance_to_date(:dated) %> </td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.