2011-05-06 3 views
4

3 단계 중첩 된 폼이 있지만 세 번째 클래스는 저장되지 않습니다.3 레벨 중첩 된 폼을 레일에 저장하는 데 문제가 있습니다.

나는 (간체) 세 가지 모델 클래스

class A 

    has_one :b 
    accepts_nested_attributes_for :b 

end 

class B 

    belongs_to :a 

    has_many :c 
    accepts_nested_attributes_for :c 

end 

과 (간체)

class C 

    belongs_to :b 

end 

내보기

<%= form_for [@a] do |f| -%> 
    <%= f.fields_for :b do |b_form| -%> 
     <%= b_form.fields_for :c do |c_form| -%> 
     <% end %> 
    <% end %> 
<% end %> 
를했습니다

def new 
    @a= A.new 
    b = @a.b = B.new 
    b.c.build 
end 

def create 
    if (@a= A.create(params[:a])).valid? 
     //flash succes 
    end 
end 

이 해시는 다음과 같습니다 컨트롤러 : {"a"=>{"title"=>"test", "body"=>"<p>test</p>\r\n<br />", "b_attributes"=>{"title"=>"testt", "c_attributes"=>{"0"=>{"title"=>"testtt"}}}}}

하지만 A와 B가 생성됩니다 . C가 아니야, 내 기록에 오류나 무언가를 던지지 않고있어.

고마워!

편집 :

솔루션 (Zabba 덕분에)

추가 attr_accessible :c_attributes

+1

'class B '에'attr_accessible : c_attributes'를 추가해보십시오. – Zabba

+0

B에서'C'와'attr_ *'의 유효성 검사? – fl00r

+0

감사합니다 Zabba, 그랬어! –

답변

2

시도 class B

attr_accessible :c_attributes 추가

(답변해야 함)

0

컨트롤러 class B에서

def new 
    @a= A.new 
    b= @a.b.build 
    b.c.build 
end 
def create 
    @a = A.new(params[:a]) 
    if @a.valid? 
    //flash succes 
    end 
end 
관련 문제