2009-08-03 75 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 

对此有何暗示?

最好的问候,

迈克

相关问题