【问题标题】:Rails 3 - multiple level nesting - getting Uninitialized Constant ErrorRails 3 - 多级嵌套 - 获取未初始化的常量错误
【发布时间】:2011-08-29 23:22:27
【问题描述】:

我对编程和 Ruby on Rails 都很陌生。我只是在尝试使用示例 2 级深度嵌套。当我跟随 Ryan 的 Scraps (http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes) 进行 1 级深度嵌套时,一切都很好,但是当我扩展到 2 级深度时,我得到了

NameError in ParentsController#new
uninitialized constant Child::Grandchild

我的模型是这样的

class Parent < ActiveRecord::Base
  has_many :children
  accepts_nested_attributes_for :children, :allow_destroy => true
end

class Child < ActiveRecord::Base
  belongs_to :parent
  has_many :grandchildren
  accepts_nested_attributes_for :grandchildren
end

class GrandChild < ActiveRecord::Base
   belongs_to :child
end

我的控制器:父级的新方法是 ->

def new
  @parent = Parent.new
  2.times do 
    child = @parent.children.build  
    2.times {child.grandchildren.build}
  end


  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @parent }
  end
end

不知道是什么错误,当我将模型修改为

class Parent < ActiveRecord::Base
  has_many :children, :through => :grandchildren
  has_many :grandchildren
  accepts_nested_attributes_for :children, :allow_destroy => true
  accepts_nested_attributes_for :grandchildren, :allow_destroy => true
end

class Child < ActiveRecord::Base
  belongs_to :parent
  has_many :grandchildren
  accepts_nested_attributes_for :grandchildren
end

class GrandChild < ActiveRecord::Base
  belongs_to :parent
  belongs_to :child
end

然后我会收到以下错误--uninitialized constant Parent::Grandchild..

我不知道这是一个愚蠢的错误还是什么......

谢谢


我已经编辑了我的问题,这是我真正的要求。而不是像前面提到的那样一次创建父母、孩子和孙子,我想先创建父母,然后再一起创建孩子和孙子。我已经编辑了上面的代码,如下所述,

我的模特:

class Parent < ActiveRecord::Base
has_many :children
has_many :grand_children
accepts_nested_attributes_for :children, :allow_destroy => true
accepts_nested_attributes_for :grand_children, :allow_destroy => true
end

class Child < ActiveRecord::Base
belongs_to :parent
has_many :grand_children
accepts_nested_attributes_for :grand_children
end

class GrandChild < ActiveRecord::Base
belongs_to :parent
belongs_to :child
end

我的孩子控制器 - 新方法:

    def new
@parent = Parent.find(params[:parent_id])
child = Child.new
child.grand_children.build
respond_to do |format|
format.html # new.html.erb
format.xml  { render :xml => @child }
end
end

我的孩子_form模板是

<%= form_for([@parent, @parent.children.build]) do |form|  %>  
<div>
<%= form.label :name %><br />
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :sex %><br />
<%= form.text_field :sex %>
</div>
<div>
<%= form.fields_for :grand_children do |grand_child_form| %>
<%= render :partial => "grand_children/form", :locals => { :form => grand_child_form}     %>
<% end %>
</div>
<% end %>

在这里我没有收到任何错误,但是当我选择新孩子时,grand_child 没有出现,

 <%= form.fields_for :grand_children do |grand_child_form| %>
  <%= render :partial => "grand_children/form", :locals => { :form => grand_child_form} %>
<% end %>

根本没有得到反映。

提前致谢

【问题讨论】:

  • 当你实例化孙子时,你试图用一个尚未保存的孩子来做。这行不通。谷歌双嵌套表单你会找到关于这个的教程。我会写更多,但这是星期天:)

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

我相信你的错误是你的协会名称,但我没有可以尝试的 rails 终端。

has_many :grandchildren

应该是

has_many :grand_children

GrandChild 是驼峰式的,grand_child 加下划线,grand_children 在桌面化时加下划线。

我相信 rails 在进行关联时会使用下划线/tableize 方法,如果您再次遇到关联问题,请在控制台中获取您的类名并执行

"GrandChild".tableize
"GrandChild".underscore

这将为您提供协会名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多