使用 :collection 时,如何减少 Rails 中此 VIEW 中的循环数量?
我正在使用 :collection 来浏览属于给定营销活动的所有联系人。
但在该活动中,我检查了三个不同的模型(每个模型都有自己的部分)。感觉就像我正在浏览 3 次联系人列表。我怎样才能让这个变得更精简?
<h2>These are past due:</h2>
<% @campaigns.each do |campaign| %>
<h3>Campaign: <%= link_to campaign.name, campaign %></h3>
<strong>Emails in this Campaign:</strong>
<% for email in campaign.emails %>
<h4><%= link_to email.title, email %> <%= email.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_email",
:collection => @contacts,
:locals => {:email => email} %>
<% end %>
Calls in this Campaign:
<% for call in campaign.calls %>
<h4><%= link_to call.title, call %> <%= call.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_call",
:collection => @contacts,
:locals => {:call => call} %>
<% end %>
Letters in this Campaign:
<% for letter in campaign.letters %>
<h4><%= link_to letter.title, letter %> <%= letter.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_letter",
:collection => @contacts,
:locals => {:letter => letter} %>
<% end %>
<% end %>
I am using the :collection to go through all the Contacts that are part of a given Campaign.
But within that Campaign I check for three different Models (each with their own partial). Feels like I am going through the list of Contacts 3x. How can I make this alot leaner?
<h2>These are past due:</h2>
<% @campaigns.each do |campaign| %>
<h3>Campaign: <%= link_to campaign.name, campaign %></h3>
<strong>Emails in this Campaign:</strong>
<% for email in campaign.emails %>
<h4><%= link_to email.title, email %> <%= email.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_email",
:collection => @contacts,
:locals => {:email => email} %>
<% end %>
Calls in this Campaign:
<% for call in campaign.calls %>
<h4><%= link_to call.title, call %> <%= call.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_call",
:collection => @contacts,
:locals => {:call => call} %>
<% end %>
Letters in this Campaign:
<% for letter in campaign.letters %>
<h4><%= link_to letter.title, letter %> <%= letter.days %> days</h4>
<% @contacts= campaign.contacts.find(:all, :order => "date_entered ASC" )%> <!--contacts collection-->
<!-- render the information for each contact -->
<%= render :partial => "contact_letter",
:collection => @contacts,
:locals => {:letter => letter} %>
<% end %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望它们按类型(联系人、电子邮件、信件等)排序,我认为您无法提取更有效的内容(仅将重复代码提取到单独的部分中并在那里渲染集合:
然后在 < code>_asset 部分,您可以使用
asset
变量来引用当前反对者并通过@contacts
实例变量联系联系人。If you want them ordered by type (contacts, emails, letters, etc.) I don't think you'll be able to pull something more efficient (only to extract the repetitive code into separate partial and render collection there:
Then inside
_asset
partial you can useasset
variable to refer to current objectr and reach contacts via@contacts
instance var.