使用 :collection 时,如何减少 Rails 中此 VIEW 中的循环数量?

发布于 2024-09-02 18:25:52 字数 2037 浏览 3 评论 0原文

我正在使用 :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2024-09-09 18:25:52

如果您希望它们按类型(联系人、电子邮件、信件等)排序,我认为您无法提取更有效的内容(仅将重复代码提取到单独的部分中并在那里渲染集合:

<% @contacts = campaign.contacts.find(...) %>
<% [:emails, :calls, :letters].each do |asset| %>
  <h4><%= asset.humanize %> in this campaign</h4>
  <%= render :partial => 'asset', :collection => campaign.send(asset), :as => :asset %>
<% end %> 

然后在 < 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:

<% @contacts = campaign.contacts.find(...) %>
<% [:emails, :calls, :letters].each do |asset| %>
  <h4><%= asset.humanize %> in this campaign</h4>
  <%= render :partial => 'asset', :collection => campaign.send(asset), :as => :asset %>
<% end %> 

Then inside _asset partial you can use asset variable to refer to current objectr and reach contacts via @contacts instance var.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文