2011-08-09 74 views
1

我试图设置一个简单的嵌套模型窗体,但在尝试通过“新”操作显示窗体时出现错误。这里是我的设置:嵌套模型表格

class Account < ActiveRecord::Base 
    has_many :people 
    has_many :organizations 

    accepts_nested_attributes_for :organizations 
end 

class Organization < ActiveRecord::Base 
    belongs_to :account 

    has_many :locations 

    accepts_nested_attributes_for :people 
    accepts_nested_attributes_for :addresses 
end 

class AccountsController < ApplicationController 

    def new 
     @account = Account.new 
     @account.organizations.build 
    end 

    def create 
     @account = Account.new(params[:account]) 
     if @account.save 
      #handle success 
     else 
      render 'new' 
     end 
    end 

end 

<%= form_for(@account) do |f| %> 

<%= f.label :type %><br /> 
<%= f.text_field :type %><br /> 

    <%= f.fields_for :organization do |organization_fields| %> 
     <%= organization_fields.label :name %><br /> 
     <%= organization_fields.text_field :name %><br /> 
     <%= organization_fields.label :website %><br /> 
     <%= organization_fields.text_field :website %><br /> 
    <% end %> 

<%= f.submit "Add account" %> 
<% end %> 

当试图击中/账户的“新”动作/新我收到以下错误:

未初始化的经常项目::组织

应用程序跟踪: app/controllers/accounts_controller.rb:5:in'new'

任何帮助将不胜感激。

回答

0

这似乎是一个奇怪的加载顺序问题。你在做什么聪明的config.load_paths或类似的东西?

只是为了查看它是否能够正常工作,请在account.rb顶部尝试require File.join(Rails.root, 'app/models/organization.rb')。这不是您想要保留的解决方案,但如果它适用于该线路,那么您将知道问题出在加载器上。

+0

我发现我的错误。在我的组织模型中,我添加了accep_nested_attributes_for:locations并且它可以工作。愚蠢的错误。谢谢你的帮助。 –