Rails 2.3 Four Layer Nesting Javascript Function

I use an example of an alloy shape. here . An example that he provides is a hierarchy of two levels, and I'm trying to expand it to four.

It has javascript functions to add nested elements. How to expand for four nested layers?

'.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) }); } 
+4
source share
3 answers

Ryan Bates made this decision easy. Works with Rails 3 with helmerj comment :)

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

helmerj comment:

For the last Rails3.rc ONe, you need to change one line:

in "module ApplicationHelper"

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

which removes the unnecessary h () function. Otherwise, it works like a charm.

0
source

I think this is a slightly overloaded form if you need to handle a three-level hierarchy. rethink your user interface first.

Other than that, I don’t think you need to create related objects in the controller, since the sample application creates them (if necessary) in the views (app / views / projects / _form.erb):

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

+1
source

Unfortunately, this is not my area of ​​expertise ...

Let me just state that I would not use the example that was used in the examples of complex shape for so much nesting. Most likely, I completely created the necessary input in JS with the usual DOM API. This should give you a much cleaner and more compact implementation, as well as a better check.

Cheers, Ela

+1
source

All Articles