Ruby on rails Cocoon Gem:未定义的方法“新记录”和“x27”;对于nil:NilClass在链接\u到\u删除\u关联上
我正试图用蚕茧宝石建造一个嵌套的形状。但是,我得到的错误如下所示。我在这里找到了另一个答案。然而,正如您从我的模型代码中看到的,唯一的答案已经被排除了 错误Ruby on rails Cocoon Gem:未定义的方法“新记录”和“x27”;对于nil:NilClass在链接\u到\u删除\u关联上,ruby-on-rails,ruby,ruby-on-rails-4,cocoon-gem,Ruby On Rails,Ruby,Ruby On Rails 4,Cocoon Gem,我正试图用蚕茧宝石建造一个嵌套的形状。但是,我得到的错误如下所示。我在这里找到了另一个答案。然而,正如您从我的模型代码中看到的,唯一的答案已经被排除了 错误 ActionView::Template::Error (undefined method `new_record?' for nil:NilClass): 1: <div class="nested-fields"> 2: <%=f.input :name%>
ActionView::Template::Error (undefined method `new_record?' for nil:NilClass):
1: <div class="nested-fields">
2: <%=f.input :name%>
3: <%= link_to_remove_association "remove task", f%>
4: </div>
app/views/templates/_room_fields.html.erb:3:in `_app_views_templates__room_fields_html_erb__1867913568926009508_70125979350780'
app/views/templates/_form.html.erb:5:in `block (2 levels) in _app_views_templates__form_html_erb__4123974558704004784_70125994949300'
app/views/templates/_form.html.erb:4:in `block in _app_views_templates__form_html_erb__4123974558704004784_70125994949300'
app/views/templates/_form.html.erb:1:in `_app_views_templates__form_html_erb__4123974558704004784_70125994949300'
app/views/templates/new.html.erb:1:in `_app_views_templates_new_html_erb___3689493092838604682_70125964273280'Models
ActionView::Template::Error(nil:NilClass的未定义方法'new_record'):
1:
2:
三:
4:
app/views/templates/_room_fields.html.erb:3:in`_app_views_templates_room_fields_html_erb_1867913568926009508_70125979350780'
app/views/templates/_form.html.erb:5:in`block(2层)in_app_view_templates_form_html_erb_4123974558704004784_701259949300'
app/views/templates/_form.html.erb:4:in'block in_app_views_templates_form_html_erb_41239745558704004784_70125994949300'
app/views/templates/_form.html.erb:1:in`_app_views_templates_form_html_erb_41239745558704004784_70125994949300'
app/views/templates/new.html.erb:1:in``应用程序\视图\模板\新的\ html \ erb \ 368949309283838604682\ u 70125964273280'模型
型号
class Template < ActiveRecord::Base
has_many :rooms
accepts_nested_attributes_for :rooms, :allow_destroy => true
end
class Room < ActiveRecord::Base
belongs_to :template
has_many :items
accepts_nested_attributes_for :items, :allow_destroy => true
end
class Item < ActiveRecord::Base
belongs_to :room
end
类模板true
结束
教室true
结束
类项
表单视图
<%= simple_form_for @template do |f| %>
<%= f.input :name%>
<div id="rooms">
<%= simple_fields_for :rooms do |room| %>
<%= render 'room_fields',:f => room %>
<%end%>
<div class="links">
<%= link_to_add_association 'add room', f, :rooms%>
</div>
</div>
<%end%>
房间%>
房间局部
<div class="nested-fields">
<%=f.input :name%>
<%= link_to_remove_association "remove task", f%>
</div>
控制器
class TemplatesController < ApplicationController
def new
@template = Template.new
end
def create
end
end
class TemplatesController
Cocoon正在尝试执行f.object.new\u record?
并且从显示的错误消息中可以清楚地看到f.object
为nil
我发现问题出在new
操作中。您已经构建了一个空白的模板
对象,但没有任何与之关联的房间
。你必须这样做-
def new
@template = Template.new
@template.rooms.build
end
这里的错误是的
简单字段没有链接到表单对象。所以写
<%= f.simple_fields_for :rooms do |room| %>
是否尝试将f
替换为@template
?我认为删除表单没有任何意义。我尝试过,但从我对代码的阅读来看,f不应该引用@template。是的,没错。我的意思是,您传递的是表单生成器,而不是对象。因此,请尝试使用f.object
访问底层模型实例。cocoon实际上调用了.object。新的\u记录?
在f
上,传递f.object
不会有帮助。