2013-10-08 1 views
0

Cocoon Gem으로 중첩 된 양식을 작성하려고합니다. 그러나 나는 아래와 같은 오류가 나타납니다. Rails Cocoon Gem: Undefined Method 'new_record?' on link_to_remove_association with Wicked이라는 또 다른 질문을 발견했습니다. 그러나 내 모델 코드에서 볼 수있는 유일한 대답은 이미 배제되었습니다.Cocoon Gem : 정의되지 않은 메소드`new_record? ' nil : link_to_remove_association의 NilClass

오류

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 

모델

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 

폼보기

<%= 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> 

누에 고치가 f.object.new_record?을 실행하기 위해 노력하고 표시되는 오류 메시지에서, f.objectnil 인 것은 분명하다

class TemplatesController < ApplicationController 
    def new 
    @template = Template.new 
    end 

    def create 
    end 
end 
+0

나는 폼을 제거하는 것이 의미가 있다고 생각하지 않는다. – phoet

+0

나는 코드를 읽는 중에 @template을 참조해서는 안되지만 시도했다. 그 방들. – gsueagle2008

+0

아 그래, 그건 사실이야. 제 말은 여러분이 물체가 아니라 폼 빌더를 전달하고 있다는 것입니다. 따라서'f.object'를 사용하여 기본 모델 인스턴스에 액세스하십시오. – phoet

답변

0

컨트롤러.

나는 문제가 new 조치에 있음을 확인했습니다. 빈 Template 개체를 만들었지 만 room 개체가 연결되어 있지 않습니다. 이 작업을 수행해야합니다 - 여기

def new 
    @template = Template.new 
    @template.rooms.build 
end 
1

오류는 simple_fields_for 양식 객체에 연결되지 않는 것입니다. 그래서`@ template`로`f`를 바꾸려고 시도 했습니까?

<%= f.simple_fields_for :rooms do |room| %> 
관련 문제