[英]Three level nested forms in rails using accepts_nested_attributes_for
我一直在嘗試在Rails應用程序中使用accepts_nested_attributes_for
實現動態多模型表單。 我一直在遵循Eloy Duran的復雜形式示例 ,該示例顯示了2級實現,但是我一直試圖將其擴展到3級。 3層表單支持accepts_nested_attributes_for
嗎? 誰能告訴我如何擴展示例應用程序?
我在第3層使用了部分javascript(由於某種原因並非總是起作用),但是我無法保存第3層的對象。 它為每個屬性傳遞的參數名稱是:
greatgrandparent[grandparent_attributes][0][parent_attributes][0][object_attributes][1249277008434][attribute]
其中grandgrandparent是表單的對象,祖父母是第一級,parent是第二級,object是第三級(我要保存的對象)。
謝謝,我感謝任何指示。
我已經更新了我的復雜形式示例以與Rails 2.3一起使用。 有關深層嵌套模型的示例,請參見深入分支 。
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
到目前為止,這是我所見過的最干凈的解決方案。 如果您發現任何錯誤或改進,請在GitHub上添加一個問題 。
首先,謝謝Ryan為您發布解決方案。 它在兩個級別的形式中確實能很好地工作,但是在更深層的嵌套形式中使用它時遇到了問題。 如果我想將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
有什么暗示嗎?
最好的祝福,
麥克風
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.