2013-02-07 28 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没有命名或放置正确,这就是为什么你得到那个错误,告诉你它找不到所说的类。

相关问题