我有三级嵌套表单,但第三个类没有保存。在轨道中保存三级嵌套表单存在问题
我有三个模型类(简体)
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_attributes
在class B
尝试在'class B'中添加'attr_accessible:c_attributes'。 – Zabba 2011-05-06 15:36:07
B中'C'和'attr_ *'的任何验证? – fl00r 2011-05-06 15:38:57
谢谢Zabba,它做到了! – 2011-05-06 15:59:20