2009-11-20 3 views
1

을 감안할 때 이러한 관계 :왜 렌더링입니까 : 부분적인 회선이 내 컬렉션을 두 번 반복합니까?

class Account < ActiveRecord::Base 
    has_many :employments 
    has_many :people, :through => :employments 
    accepts_nested_attributes_for :employments 
end 

class Employment < ActiveRecord::Base 
    belongs_to :account 
    belongs_To :person 
end 

내가 계정에 대한 고용 기록 목록을하기 위해 노력하고있어 :

<% form_for @account do |f| -%> 
    <% f.fields_for :employments do |e| -%> 
    <%= render :partial => 'employment', :collection => @account.employments, :locals => { :f => e } %> 
    <% end -%> 
<% end -%> 

내가 @account의 고용 테이블에 두 개의 레코드가 포함되어 있는지 확인한를하지만 두 번씩 고용을 반복하기 때문에 부분 사본을 4 부 받아야합니다.

Employment Load (1.0ms) SELECT * FROM [employments] WHERE ([employments].account_id = 1) 
Person Load (1.3ms) SELECT * FROM [people] WHERE ([people].[id] = 2) 
Rendered accounts/_employment (17.9ms) 
Person Load (1.5ms) SELECT * FROM [people] WHERE ([people].[id] = 1) 
Rendered accounts/_employment (5.1ms) 
Rendered accounts/_employment (2.2ms) 
Rendered accounts/_employment (2.1ms) 

아무도 왜 그렇게 될지 설명 할 수 있습니까?


여기에 몇 가지 추가 정보입니다 :

_employment.html.erb 부분 :

<div class="employment"> 
    <span class="name"><%= link_to h(employment.person.name), person_path(employment.person) %></span> 
    <span class="role"><%=h employment.role %></span> 
    <span class="commands"><%= remove_child_link "Remove", f %></span> 
</div> 

remove_child_link 내가에서 양식 필드를 생성 할 필요가있는 유일한 장소입니다. 레코드에 대해 _delete 필드를 만들고 값을 '1'로 변경하는 제거 링크를 연결합니다. 또한 'role'속성을 편집 할 수도 있습니다. 중요한 것은 모든 필드를 편집 할 수 없기를 바랍니다. 이 뷰

accounts_controller 행동은 :

def edit 
    @account = Account.find(params[:id]) 
end 

def update 
    @account = Account.find(params[:id]) 

    respond_to do |format| 
    if @account.update_attributes(params[:account]) 
     flash[:notice] = "#{@account.business_name} was successfully updated." 
     format.html { redirect_to @account } 
    else 
     format.html { render :action => "edit" } 
    end 
    end 
end 

벤은 내가 올바른 방향으로 가고 있어요. 일부 런타임 검사는 레코드가 object 변수에 저장되었음을 보여줍니다 (이미 알고 있지만 다른 컨텍스트에서). 그래서 같은 fields_for 절 다시 작성할 수 있습니다 :

<% form_for @account do |f| -%> 
    <% f.fields_for :employments do |e| -%> 
    <div class="employment"> 
     <span class="name"><%= link_to h(e.object.person.name), person_path(e.object.person) %></span> 
     <span class="role"><%=h e.object.role %></span> 
     <span class="commands"><%= remove_child_link "Remove", e %></span> 
    </div> 
    <% end -%> 
<% end -%> 

답변

1

당신은 fields_for를 사용하는 것이 정확하지만, 각 employment을 위해 그 안에 무엇을 렌더링, 그래서 render :partial에서 :collection 매개 변수를 제거 : 즉, fields_for를 통해 반복을 제거한다. http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

+0

이미 거기에'accepts_nested_attributes_for'를 사용하고 있습니다. 죄송합니다. 나는 그것을 예제에 포함시키는 것을 잊었습니다. 그러나': collection' 매개 변수를 제거하면 부분적으로'nil.person'을 평가하려고 시도합니다. –

+0

부분적으로 사용하지 않을 때 작동합니까? 'fields_for'에 중첩 된 필드를 붙이면됩니까? – Ben

+0

필드를 작성하는 것이 아니라 편집 가능한 특정 필드, 특히 삭제 필드 만있는 기존 고용 기록을 표시하려고합니다. 'field_for' 안에 일반 html로 필드를 표시 할 수 있습니까? 내가 각 레코드에 대한 지역 변수를 주었기 때문에'render : partial'을 사용하고있었습니다. –

3

그 렌더링을 그 고용 모델의 각 필드에 대한 부분 - 정말 각 고용 레코드에 대해 한 번 수행 할 때.

<% form_for @account do |f| -%> 
    <%= render :partial => 'employment', :collection => @account.employments %> 
<% end -%> 
+0

fields_for가 반복자입니다 :

accepts_nested_attributes_for :employments 

여기에 중첩 된 형식에 대한 자세한 내용 대신, 계정 모델이 넣어 중첩 된 양식을 사용? 나는 단지 폼 빌더의 이름 생성을 변경하지만. –