2011-05-06 57 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