0

1つに統合したい3つのパーシャルがあります。それらは同じコレクションを共有しますが、それぞれに独自の:local変数が渡されます。これらの変数は特定のモデルに使用されるため、結果として、パーシャルと3つの異なるパーシャルに対して3つの異なる呼び出しがあります。

繰り返しコードは次のとおりです。

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

パーシャルの1つの例は次のとおりです。

<

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」(_ 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で指定されたメソッドが2つのパラメーターを効果的に受け取ることを確認してください。

于 2010-05-28T21:40:59.970 に答える