Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3.1充当树-确定子级?关于FormBuilder对象_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails Rails 3.1充当树-确定子级?关于FormBuilder对象

Ruby on rails Rails 3.1充当树-确定子级?关于FormBuilder对象,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我正试图找出最好的方法。我需要通过一个acts_as_树模型(下面的MenuHeader)递归地向下钻取。我有以下型号: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers end class MenuHeader < ActiveRecord::Base belongs_to :menu acts_as_tree :pa

我正试图找出最好的方法。我需要通过一个acts_as_树模型(下面的MenuHeader)递归地向下钻取。我有以下型号:

class Menu < ActiveRecord::Base
  has_many :menu_headers
  accepts_nested_attributes_for :menu_headers
end

class MenuHeader < ActiveRecord::Base
  belongs_to :menu
  acts_as_tree :parent_id
  has_many :menu_items
  accepts_nested_attributes_for :menu_items
end

class MenuItem < ActiveRecord::Base
  belongs_to :menu_header
  has_one :price
end
和实际形式(问题见#####)

在父菜单中获取表单值-正在复制第二个菜单标题#4

menu 1 - menu[content]
mh 1, parent_id=nil - menu[menu_header_attributes][0][name]
    mi 1 - fk menu_header 1 - menu[menu_headers_attributes][0][menu_items_attributes][0][name]
mh 2 - parent_id=nil - menu[menu_headers_attributes][1][name]
    mi2 - fk menu_header_id=2 - menu[menu_headers_attributes][1][menu_items_attributes][0][name]
    mi3 - fk menu_header_id=2 - menu[menu_headers_attributes][1][menu_items_attributes][1][name]
  mh 3 parent_id=2 - menu[menu_headers_attributes][1][children_attributes][0][name]
      mi 4, menu_header_id=3 - menu[menu_headers_attributes][1][children_attributes][0][menu_items_attributes][0][name]
  mh 4 parent_id=2 menu[menu_headers_attributes][1][children_attributes][1][name]
    mh 5 parent_id=4 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][name]
        mi 5, menu_header_id=5 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][menu_items_attributes][0][name]
    mh 6 parent_id=5 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][children_attributes][0][name]
  mh 4 parent_id=2 menu[menu_headers_attributes][2][name]
    mh 5 parent_id=4 menu[menu_headers_attributes][2][children_attributes][0][name]
        mi 5, menu_header_id=5 menu[menu_headers_attributes][2][children_attributes][0][menu_items_attributes][0][name]
      mh 6 parent_id=5 menu[menu_headers_attributes][2][children_attributes][0][menu_items_attributes][0][name]
    mh 5 parent_id=4 - menu[menu_headers_attributes][3][name]
        mi 5, menu_header_id=5 - menu[menu_headers_attributes][3][menu_items_attributes][0][name]
      mh 6 parent_id=5 menu[menu_headers_attributes][3][children_attributes][0][name]

看看这里发生了什么-就像孩子们的属性有点半危险。有没有基于代码的想法发生了什么?我应该提到的是,更新发生在万岁

哎呀,误读了那个

我相信
将为您提供直接子级的集合,然后您必须递归调用每个子级(在您的空间中)的
,以获得整个树

您可能可以
接受\u嵌套的\u属性\u for:children
以允许您设置它们的属性

编辑 这种方法怎么样:

<%= form_for(@menu) do |f| %>
  <div class="actions">
    <%= f.text_field :content %><br />
    <%= f.fields_for :menu_headers do |mh| %>
      <%= render :partial => 'menu_headers/form', :object => mh %>
    <% end %>
    <%= f.submit %>
  </div>
<% end %>
并加上:


接受子对象的嵌套属性。\u
到您的
菜单头
模型。

深度优先遍历?有可能是你的数据吗?你能用手确认这些关系吗,只是为了确保你没有重复的行?y-我想你是对的-让我做一些验证,我会删除上面的编辑-虽然我很高兴看到所有这些都是如何在表单中命名的。至少有一些魔力。真棒的工作-我看到了mh.object.children的问题,但没有完全得到你的答案-对meDoes来说,这是一个未知的领域,更有意义吗?编辑上面很酷-有道理(有点),但孩子的数量可能有问题。它复制了一些将在一秒钟内使用表单输入值进行更新
<%  if mh.object.children.exists? %>
    <%= render :partial => 'children' %>
<% end %>
menu 1
  mh 1, parent_id=nil
      mi 1 - fk menu_header 1
  mh 2 - parent_id=nil
      mi2 - fk menu_header_id=2
      mi3 - fk menu_header_id=2
    mh 3 parent_id=2 
        mi 4, menu_header_id=3
    mh 4 parent_id=2 
      mh 5 parent_id=4
        mi 5, menu_header_id=5
      mh 6 parent_id=5
menu 1 - menu[content]
mh 1, parent_id=nil - menu[menu_header_attributes][0][name]
    mi 1 - fk menu_header 1 - menu[menu_headers_attributes][0][menu_items_attributes][0][name]
mh 2 - parent_id=nil - menu[menu_headers_attributes][1][name]
    mi2 - fk menu_header_id=2 - menu[menu_headers_attributes][1][menu_items_attributes][0][name]
    mi3 - fk menu_header_id=2 - menu[menu_headers_attributes][1][menu_items_attributes][1][name]
  mh 3 parent_id=2 - menu[menu_headers_attributes][1][children_attributes][0][name]
      mi 4, menu_header_id=3 - menu[menu_headers_attributes][1][children_attributes][0][menu_items_attributes][0][name]
  mh 4 parent_id=2 menu[menu_headers_attributes][1][children_attributes][1][name]
    mh 5 parent_id=4 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][name]
        mi 5, menu_header_id=5 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][menu_items_attributes][0][name]
    mh 6 parent_id=5 menu[menu_headers_attributes][1][children_attributes][1][children_attributes][0][children_attributes][0][name]
  mh 4 parent_id=2 menu[menu_headers_attributes][2][name]
    mh 5 parent_id=4 menu[menu_headers_attributes][2][children_attributes][0][name]
        mi 5, menu_header_id=5 menu[menu_headers_attributes][2][children_attributes][0][menu_items_attributes][0][name]
      mh 6 parent_id=5 menu[menu_headers_attributes][2][children_attributes][0][menu_items_attributes][0][name]
    mh 5 parent_id=4 - menu[menu_headers_attributes][3][name]
        mi 5, menu_header_id=5 - menu[menu_headers_attributes][3][menu_items_attributes][0][name]
      mh 6 parent_id=5 menu[menu_headers_attributes][3][children_attributes][0][name]
<%= form_for(@menu) do |f| %>
  <div class="actions">
    <%= f.text_field :content %><br />
    <%= f.fields_for :menu_headers do |mh| %>
      <%= render :partial => 'menu_headers/form', :object => mh %>
    <% end %>
    <%= f.submit %>
  </div>
<% end %>
  <%= form.text_field :name %><br />
  <%= form.fields_for :menu_items do |mi| %>
    <%= mi.text_field :name %><br />
  <% end %>

  <%= form.fields_for :children do |mh| %>
     <%= render :partial => 'form', :object => mh %>
  <% end %>