2009-11-20 65 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就业表包含两个记录,但我得到了部分的四个副本,因为它遍历就业两次:

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'的删除链接。但是,'角色'属性也可以编辑。重要的是我不希望所有的字段都是可编辑的。

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参数。相反,通过把这个在您的Account模型使用嵌套形式:

accepts_nested_attributes_for :employments 

阅读更多关于嵌套形式在这里:http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

+0

我已经在那里使用了'accep_nested_attributes_for',对不起,我忘了将它包含在我的例子中。但是,当我删除':collection'参数时,它会尝试评估部分中的'nil.person'。 – 2009-11-20 17:26:28

+0

当你不使用部分时它会工作吗?只需在'fields_for'中嵌入嵌套字段? – Ben 2009-11-20 18:15:37

+0

我只想显示现有的工作记录,只有某些字段可编辑,特别是删除字段。是否可以在'field_for'内将字段显示为纯HTML?我正在使用'render:partial',因为这给了我每个记录的局部变量。 – 2009-11-20 18:29:24

3

它呈现的是部分在就业模型中的每个领域 - 当你真的需要为每个就业记录做一次。也就是说,除去遍历所有fields_for:

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

fields_for是一个迭代器?我虽然只是改变了表单构建器的名称代。 – 2009-11-20 01:04:36