0

我想将三个部分合并为一个。它们共享同一个集合,但每个都传递了自己的 :local 变量。这些变量用于特定的模型,因此,我对部分和三个不同的部分进行了三个不同的调用。

这是重复的代码:

<% 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 %>

其中一个部分的示例如下:

<

div id="contact_email_partial">
 <% if from_today(contact_email, email.days) < 0 %>
       <% if show_status(contact_email, email) == 'no status'%>
            <p> <%= full_name(contact_email) %>
                <% unless contact_email.statuses.empty?%>
                    (<%= contact_email.statuses.find(:last).status%>) 
                 <% end %>
                is <%= from_today(contact_email,email.days).abs%> days overdue:
                <%= do_event(contact_email, email) %>

                <%= link_to_remote "Skip Email Remote",
                                  :url => skip_contact_email_url(contact_email,email),
                                  :update => "update-area-#{contact_email.id}-#{email.id}" %>
                <span id='update-area-<%="#{contact_email.id}-#{email.id}"%>'> </span>
        <% end %>
     <% end %>
</div>

这是另一个部分......类似的,是吗? 需要帮助使其干燥!

 <% if (from_today(contact_call, call.days) < 0) %>
       <% if show_status(contact_call, call) == 'no status'%>
            <p> <%= full_name(contact_call) %> 
                 <% unless contact_call.statuses.empty?%>
                    (<%= contact_call.statuses.find(:last).status%>) 
                 <% end %>
                is <%= from_today(contact_call,call.days).abs%> days overdue:
                <%= do_event(contact_call, call) %>
                <%= contact_call.phone %>
            </p>
        <% end %>
     <% end %>
4

1 回答 1

0

对于局部变量,您在传递局部哈希时选择的键是变量在部分中的名称。因此,您可以在重复代码中执行此操作:

<% 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",
                :collection => @contacts,
                :locals => {:objt => email, 
                            :url_method => "skip_#{class}_url".to_sym } %>
<% 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",
                :collection => @contacts,
                :locals => {:objt => call,
                            :url_method => "skip_#{class}_url".to_sym } %>
   <% 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",
                :collection => @contacts,
                :locals => {:objt => letter,
                            :url_method => "skip_#{class}_url".to_sym } %>

   <% end %>

由于在部分中我们并不真正知道我们需要渲染的对象的类,因此我正在创建一个符号,表示您需要调用以构建 URL 的方法。我们将使用一些发送魔法在部分中调用它。

请注意,部分已被简单地重命名为“联系人”(_contact.html.erb),我将电子邮件、电话和信件依次分配给名为 objt 的变量,该变量可从部分访问。

对于部分(_contact.html.erb):

<div id="contact_partial">
 <% if from_today(contact, objt.days) < 0 %>
       <% if show_status(contact, objt) == 'no status'%>
            <p> <%= full_name(contact) %>
                <% unless contact.statuses.empty?%>
                    (<%= contact.statuses.find(:last).status%>) 
                 <% end %>
                is <%= from_today(contact,objt.days).abs%> days overdue:
                <%= do_event(contact, objt) %>

                <%= link_to_remote "Skip Email Remote",
                                  :url => send(url_method,contact,objt), 
                                  :update => "update-area-#{contact.id}-#{objt.id}" %>
                <span id='update-area-<%="#{contact.id}-#{objt.id}"%>'> </span>
        <% end %>
     <% end %>
</div>

请注意,我们不是直接调用 skip_email_url 方法,而是使用“send”来调用在第一个参数中命名的方法(在本例中,url_method,从调用视图传递的本地方法),此外,我们将附加的传递给该方法参数(在本例中为contact 和objt)。只需确保 url_method 中命名的方法确实有效地采用了两个参数。

于 2010-05-28T21:40:59.970 回答