2011-05-06 44 views
4

我有三級嵌套表單,但第三個類沒有保存。在軌道中保存三級嵌套表單存在問題

我有三個模型類(簡體)

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不,這不是trowing錯誤或東西在我的日誌..

謝謝!

編輯

溶液(感謝Zabba)

添加attr_accessible :c_attributesclass B

+1

嘗試在'class B'中添加'attr_accessible:c_attributes'。 – Zabba 2011-05-06 15:36:07

+0

B中'C'和'attr_ *'的任何驗證? – fl00r 2011-05-06 15:38:57

+0

謝謝Zabba,它做到了! – 2011-05-06 15:59:20

回答

2

嘗試在class B

加入

(應該回答)

0

控制器

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