2009-08-03 77 views
7

我一直在試圖在我的rails應用程序中使用accepts_nested_attributes_for實現動態多模式表單。我一直在關注Eloy Duran的complex-forms example,它顯示了2級的實現,但我一直試圖將其擴展到3個級別。是否支持accepts_nested_attributes_for 3級表格?任何人都可以告訴我如何擴展示例應用程序?在rails中使用accept_nested_attributes_for的三級嵌套表單

我已經得到了JavaScript部分工作(不總是工作的原因)爲第三級,但我不能保存第三級對象(S)。它是通過爲每個屬性PARAM NAME是:

greatgrandparent[grandparent_attributes][0][parent_attributes][0][object_attributes][1249277008434][attribute] 

凡greatgrandparent是對象的形式爲,祖父母是第一電平,父是第二電平,並且對象是第三電平(一個我試圖保存)。

謝謝,我很欣賞任何指針。

回答

18

我已更新我的complex-form-examples以使用Rails 2.3。有關深度嵌套模型的示例,請參閱deep branch

git clone git://github.com/ryanb/complex-form-examples.git 
cd complex-form-examples 
git checkout -b deep origin/deep 
rake db:migrate 
script/server 

這是迄今爲止我見過的最乾淨的解決方案。如果您發現任何錯誤或改進,請撥打add an issue on GitHub

+0

你是一個生命的救星。 – Anon 2009-08-05 03:33:02

2

首先感謝瑞安張貼您的解決方案。 它在兩個級別的表單中工作得很好,但是我在使用更深的嵌套表單時遇到了問題。 如果我想將add_child_link放入另一個已經添加的部分,Firebug會給我一個javascript錯誤。似乎有一個逃跑的錯誤。

我已經嘗試避免通過將另一個選項傳遞給鏈接方法來轉義內部部分,但這種方式不起作用。

def add_child_link(name, f, options) 
    fields = new_child_fields(f, options) 
    fields = escape_javascript(fields) unless options[:already_escaping] == true 
    link_to_function name, %{ 
     var new_object_id = new Date().getTime(); 
     var html = jQuery("#{fields}".replace(/#{options[:index]}/g, new_object_id)).hide(); 
     html.appendTo(jQuery("#{options[:where]}")).show(); 
     } 
end 

對此有何暗示?

最好的問候,

邁克

相關問題