我有三級嵌套表單,但第三個類沒有保存。在軌道中保存三級嵌套表單存在問題
我有三個模型類(簡體)
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