2013-02-07 27 views
4

我創建customer_billcustomer_bill_line_item之間的關聯如下:未初始化的常量CustomerBill :: CustomerBillLineItem

class CustomerBill < ActiveRecord::Base 
    attr_accessible :customer_bill_line_items_attributes 
    has_many :customer_bill_line_items, :dependent =>:destroy 

    accepts_nested_attributes_for :customer_bill_line_items, :allow_destroy => true 
end 

class CustomerBillLineItem < ActiveRecord::Base 
    attr_accessible :customer_bill_id 
    belongs_to :customer_bill, :foreign_key => "customer_bill_id" 
end 

當我進入一種形式,創建模式,我得到以下錯誤:

uninitialized constant CustomerBill::CustomerBillLineItem 

Extracted source (around line #66): 

63:        <%end%> 
64:        
65:        
66:    <%= f.fields_for :customer_bill_line_items do |builder| %> 
67:    <%= render 'customer_bill_line_item_fields', :f => builder %> 
68:    <%end%> 

的完整的堆棧跟蹤在評論中給出。

是否有一個協會,必須在customer_bills_controller,如@customer_bill.customer_bill_line_items

需要指導。提前致謝。

+2

你可以添加一個鏈接到顯示堆棧跟蹤的要點嗎? – jvnill

+0

http://pastebin.com/NNTbFZV5: - 這是事件 – bharath

+0

的全部痕跡,你還可以在問題和突出顯示第66行'app/views/customer_bills/_form.html.erb'上包含以下文件。 – jvnill

回答

4

我很快舉出了一個示例應用程序,以證明您所做的是正確的,您可以在這裏查看:https://github.com/Bram--/customer_bill哪些工作正常。 只要確保你旋轉它,你有一個客戶比爾& CustomerBillLineItems前:

c = CustomerBill.create name: 'Name' 
CustomerBillLineItem.create name: 'Line Item A', price: '1.00', customer_bill_id: c.id 
CustomerBillLineItem.create name: 'Line Item B', price: '2.00', customer_bill_id: c.id 

什麼是您使用的版本,還有什麼我們沒有在上面的代碼看?

希望這個例子有幫助,否則就給我一條線。

+0

我正在使用rails 3.2.10。其實bill.rb中有一個非常愚蠢的錯誤。在'attr_accessible'中添加了兩次'customer_bill_line_item_attibutes'。兩個人在同一個文件上工作導致這個錯誤。 感謝您提供的見解。更好地理解代碼。 – bharath

3

你問:

Is there an association that must be made in customer_bills_controller [email protected]_bill.customer_bill_line_items??

根據我們的工作樣機由新星它沒有(從customer_bills_controller.rb,Novae's mock):

class CustomerBillsController < ApplicationController 
    def show 
    @customer_bill = CustomerBill.last 
    end 

    def update 
    @customer_bill = CustomerBill.find params[:id] 
    @customer_bill.update_attributes!(params[:customer_bill]) 

    redirect_to @customer_bill, flash: { notice: 'Updated' } 
    end 
end 

用機器人指出的差異,在他customer_bill_line_item.rb模型Novae包含更多CustomerBillLineItem屬性attr_accessible(來自app/models /):

class CustomerBillLineItem < ActiveRecord::Base 
    attr_accessible :customer_bill_id, :name, :price 
    belongs_to :customer_bill, :foreign_key => "customer_bill_id" 
end 

我無法想象這些如何導致您的錯誤,但他們是我所能找到的。

+0

其實bill.rb中有一個非常愚蠢的錯誤。在客戶bill.rb的'attr_accessible'中加了兩次'customer_bill_line_item_attibutes'。兩個人在同一個文件上工作導致這個錯誤。 感謝您澄清我在控制器中添加'@ customer_bill.customer_bill_line_items'的疑問。更好地理解代碼。 +1的答案 – bharath

2

錯誤告訴你問題是什麼。沒有可以找到的類CustomerBill :: CustomerBillLineItem。

1:我假設你不在customer_bills#new action中建立customer_bill_line_item的實例,否則你會在那裏看到同樣的錯誤。

請通過檢查你正在構建customer_bill_line_item的情況下,對@customer_bill在新的行動像

3.times{@customer_bill.customer_bill_line_items.build} 

如果再次出現同樣的錯誤,但在你的控制器建立的線就證實了確認錯誤是說它是不能找到類CustomerBillLineItem通過CustomerBill

我懷疑在類CustomerBillLineItem的文件名中的拼寫錯誤。確保您的課程位於名爲customer_bill_line_item.rb的文件中,並位於您的模型文件夾中,而不是嵌套在任何其他文件夾中。可能的範圍也是一個問題。

底線是CustomerBillLineItem沒有命名或放置正確,這就是爲什麼你得到那個錯誤,告訴你它找不到所說的類。

相關問題