【问题标题】:Rails 2.3 Four Level Nested Form Javascript FunctionRails 2.3 四级嵌套表单Javascript函数
【发布时间】:2009-04-12 10:29:10
【问题描述】:

我正在使用 here 找到的 Alloy 的复杂形式示例。他提供的示例是两级层次结构,我正在尝试将其扩展到四级。

他有用于添加嵌套项的 javascript 函数。四层嵌套如何展开?

'.add_nested_item': function(e){
   el = Event.findElement(e);
   template = eval(el.href.replace(/.*#/, ''))
   $(el.rel).insert({    
     bottom: replace_ids(template)
   });
 },
 '.add_nested_item_lvl2': function(e){
   el = Event.findElement(e);
   elements = el.rel.match(/(\w+)/g)
   parent = '.'+elements[0]
   child = '.'+elements[1]

   child_container = el.up(parent).down(child)    
   parent_object_id = el.up(parent).down('input').name.match(/.*\[(\d+)\]/)[1]

   template = eval(el.href.replace(/.*#/, ''))

   template = template.replace(/(attributes[_\]\[]+)\d+/g, "$1"+parent_object_id)

   // console.log(template)
    child_container.insert({     
     bottom: replace_ids(template)
    });
 }

【问题讨论】:

    标签: ruby-on-rails ruby forms nested-forms


    【解决方案1】:

    如果您需要处理三层层次结构,我认为它有点重载。首先重新考虑你的用户界面。

    除此之外 - 我认为您不需要在控制器中构建关联对象,因为示例应用程序在视图 (app/views/projects/_form.erb) 中构建它们(如果需要):

    <% @project.build_author unless @project.author %> <% project_form.fields_for :author do |author_form| %>

    【讨论】:

    • 感谢您指出应用程序构建对象。我更新了我的问题。
    【解决方案2】:

    很遗憾,这也不是我真正的专业领域……

    让我声明一下,我不会使用 complex-form-examples 中使用的示例进行如此多的嵌套。相反,我会使用常规 DOM API 完全在 JS 中构建所需的表单输入。这应该会给你一个更干净、更精简的实现,并且可以更好地测试。

    干杯, 埃洛伊

    【讨论】:

      【解决方案3】:

      Ryan Bates 让这个解决方案变得简单。与 helmerj 的评论一起使用 Rails 3 :)

      http://railscasts.com/episodes/197-nested-model-form-part-2

      helmerj 的评论:

      对于最新的 Rails3.rc 一个必须修改一行:

      在“模块ApplicationHelper”中

      link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")

      它删除了不需要的 h() 函数。否则就像魅力一样。

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多