简体   繁体   English

使用accepts_nested_attributes_for在rails中的三层嵌套表单

[英]Three level nested forms in rails using accepts_nested_attributes_for

I've been trying to implement a dynamic multi-model form with accepts_nested_attributes_for in my rails application. 我一直在尝试在Rails应用程序中使用accepts_nested_attributes_for实现动态多模型表单。 I've been following Eloy Duran's complex-forms example that shows a 2 level implementation, but I've been trying to expand this to 3 levels. 我一直在遵循Eloy Duran的复杂形式示例 ,该示例显示了2级实现,但是我一直试图将其扩展到3级。 Is there support with accepts_nested_attributes_for for a 3 level form? 3层表单支持accepts_nested_attributes_for吗? Can anyone show me how to expand the example app? 谁能告诉我如何扩展示例应用程序?

I've got the javascript partially working (doesn't always work for some reason) for the 3rd level, but I can't save the 3rd level object(s). 我在第3层使用了部分javascript(由于某种原因并非总是起作用),但是我无法保存第3层的对象。 The param name it is passing for each attribute is: 它为每个属性传递的参数名称是:

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

Where greatgrandparent is the object the form is for, grandparent is the first level, parent is the second level, and object is the 3rd level (the one I'm trying to save). 其中grandgrandparent是表单的对象,祖父母是第一级,parent是第二级,object是第三级(我要保存的对象)。

Thanks, I appreciate any pointers. 谢谢,我感谢任何指示。

I have updated my complex-form-examples to work with Rails 2.3. 我已经更新了我的复杂形式示例以与Rails 2.3一起使用。 See the deep branch for an example on deeply nested models. 有关深层嵌套模型的示例,请参见深入分支

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

It is the cleanest solution I've seen so far. 到目前为止,这是我所见过的最干净的解决方案。 If you find any bugs or improvements please add an issue on GitHub . 如果您发现任何错误或改进,请在GitHub上添加一个问题

First of all thank you Ryan for posting you solution. 首先,谢谢Ryan为您发布解决方案。 It does work very well in a two level form, but I've got problems using it in a deeper nested form. 它在两个级别的形式中确实能很好地工作,但是在更深层的嵌套形式中使用它时遇到了问题。 Firebug gives me a javascript error if I want to put an add_child_link into another already added partial. 如果我想将add_child_link放入另一个已经添加的局部中,Firebug给了我一个JavaScript错误。 There seems to be a escaping bug. 似乎有一个逃避的错误。

I already tried to avoid escaping the inner partial by passing another option to the link method, but this somehow doesn't work. 我已经尝试通过将另一个选项传递给链接方法来避免转义内部部分,但这在某种程度上是行不通的。

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

Any hints on that? 有什么暗示吗?

Best regards, 最好的祝福,

Mike 麦克风

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM