2013-01-05 83 views
1

我有三種模型,用戶,報表和收據。用戶有很多報告,報告有很多收據。嵌套資源表單上的未初始化的常量

現在,我創建了一個窗體,用於創建或編輯報表。我需要嵌套另一個表單來創建和編輯收據。我遵循導軌指南(部分 - 構建一個多模型表單)並編輯我的模型,並將構建行添加到我的表單視圖中,但是我得到了'未初始化常量'錯誤。

這裏是我的模型:

class Report < ActiveRecord::Base 
    belongs_to :user 
    has_many :receipts 

    attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name, 
    :start_date, :receipts_attributes 

    validates_presence_of :company, :description, :end_date, :report_name#, :start_date 
    validates_uniqueness_of :report_name 

    accepts_nested_attributes_for :receipts, :allow_destroy => :true, 
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } 
end 

class Receipts < ActiveRecord::Base 
    belongs_to :report 
    attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor 
end 

和我的形式:

<%# @report.receipts.build %> 
<%= form_for([current_user,@report]) do |f| %> 
    ... 
    <%= f.fields_for ([@report, @report.receipts.build ]) do |receipt| %> 
    ... 
    <% end %> 
<% end %> 

我的路線(其中林不知道我是否應該修改,但在此之前我增加了我得到了同樣的錯誤收據資源)

resources :users do 
    resources :reports do 
     resources :receipts 
    end 
end 

我沒有編輯報告控制器,因爲導軌指南沒有顯示任何提及它,它其唯一的:

高清新 @report = current_user.reports.new 結束

高清編輯 @report = current_user.reports.find(PARAMS [:編號]) 結束

上午什麼我做錯了?

編輯 - 我改變了我的形式收據所以的form_for發生在[@report,@ report.receipts.build],但現在我得到的錯誤:

uninitialized constant Report::Receipt 

如何獲得此表格工作?

+0

在報告模型的'attr_accessible'處將':receipt_attributes'更改爲':receiptts_attributes'。 – VenkatK

+0

我認爲那只是爲了保存記錄,因爲改變它並不能防止異常錯誤。 – rugbert

回答

7

UGH!當我生成模型並給它一個複數名稱而不是單數名稱時,我搞砸了。這個人,在這裏,是個傻瓜。

+1

太好了。我正要發佈一個很長的問題,看到你自己回答了一個問題,就像是nahhhh我並不那麼笨。但我是。去我們 –

+0

把我自己添加到愚蠢的程序員列表中...我很慚愧。 – Kevin