2012-05-05 3 views
0

fields_for 블록이 has_many 관계로 출력되지 않습니다. 이 문제는 제가 작업하고있는 다소 복잡한 프로젝트에서 나타났습니다. 나는 이것을 아주 간단한 테스트 케이스로 나누었지만 여전히 작동하지 않는다. 이 질문은 이전에 묻어 왔으며 문제는 대개 중첩 된 객체가 존재하지 않는 이었습니다. 하지만 여기에서는 코드 주석에 설명 된대로 중첩 된 객체가 존재하는 것처럼 보입니다. 나는 레일에 꽤 새로 워서 뭔가 분명 할 수 있습니다. 여기 fields_for has_many가 출력되지 않습니다.

는 간단한 케이스 모델 코드 :

class Parent < ActiveRecord::Base 
    has_many :children 
    accepts_nested_attributes_for :children 
end 

class Child < ActiveRecord::Base 
    belongs_to :parent 
end 

간단한 경우 컨트롤러 :

class ParentController < ApplicationController 
    def index 
    @parent = Parent.find_by_id(1) 
    end 
end 

간단한 사례보기 : 나는 또한이 시도하고 동일한 결과를보고

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %> 

    <!-- this outputs ok --> 
    <%= f.text_field :name %> 

    <% f.object.children.each do |c| %> 

     <!-- this outputs "child1", so the nested object exists --> 
     <%= c.name %> 

     <% f.fields_for c do |field| %> 
      this line does NOT output, nor does the field below 
      <%= field.text_field :name %> 
     <% end %> 
    <% end %> 
<% end %> 

:

<%= form_for @parent, {:url=>{:action=>:index}} do |f| %> 
    <%= f.text_field :name %> 
    output here  
    <% f.fields_for :children do |field| %> 
     no output here nor the field below 
     <%= field.text_field :name %> 
    <% end %> 
<% end %> 

또한 컨트롤러에 새로운 @parent 개체가 있고 @parent.build_child (변경된 집합은 has_one)입니다. 그것은 여전히 ​​같은 결과를 보았다.

답변

1

= 기호를 <% 뒤에 넣는 것을 잊었습니다.

은 교체 :

<% f.fields_for :children do |field| %> 

로 :

<%= f.fields_for :children do |field| %> 
+0

해결! 그게 분명했다 ... 고마워, 너의 시간을. 레 가드 – ddndentJohn

관련 문제