鉴于这些关系:为什么渲染:部分线迭代我的集合两次?
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 -%>
我已经在那里使用了'accep_nested_attributes_for',对不起,我忘了将它包含在我的例子中。但是,当我删除':collection'参数时,它会尝试评估部分中的'nil.person'。 – 2009-11-20 17:26:28
当你不使用部分时它会工作吗?只需在'fields_for'中嵌入嵌套字段? – Ben 2009-11-20 18:15:37
我只想显示现有的工作记录,只有某些字段可编辑,特别是删除字段。是否可以在'field_for'内将字段显示为纯HTML?我正在使用'render:partial',因为这给了我每个记录的局部变量。 – 2009-11-20 18:29:24