How can I make three partials into just one in rails where the :collection is the same?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-28T21:26:05Z Indexed on 2010/05/28 21:32 UTC
Read the original article Hit count: 155

I have three partials that I'd like to consolidate into one. They share the same collection, but each gets passed its own :local variable. Those variables are used for specific Models, so as a result, I have three different calls to the partial and three different partials.

Here's the repetitive code:

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

An example of one of the partials is as follows:

<

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>

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about collections