2011-08-09 3 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 %> 

계정 /에서 '새로운'조치를 칠 시도/I는 다음과 같은 오류를 얻고 새로운 :

초기화되지 않은 상수 계정 : 조직

응용 프로그램 추적 : app/controllers/accounts_controller.rb : 5 : 'new'

모든 도움을 주실 수 있습니다.

답변

0

이것은 홀수로드 순서 문제로 나타납니다. config.load_paths 또는 그와 비슷한 것을 사용하여 영리하게 수행하고 있습니까?

account.rb 상단에 require File.join(Rails.root, 'app/models/organization.rb')을 시도해보십시오. 이것은 주위에 보관하고 싶은 해결책은 아니지만, 해당 라인에서 작동하는 경우 문제가 로더에 있음을 알 수 있습니다.

+0

오류가 발견되었습니다. 내 조직 모델에서 accepts_nested_attributes_for : 위치를 추가하고 작동합니다. 어리석은 실수. 당신의 도움을 주셔서 감사합니다. –