2010-05-28 2 views
0

나는 하나로 통합하고 싶은 부분이 세 개 있습니다. 그들은 같은 콜렉션을 공유하지만, 각각은 로컬 변수를 전달받습니다. 이러한 변수는 특정 모델에 사용되므로 결과적으로 부분 및 세 가지 부분에 대해 세 가지 다른 호출이 있습니다.: 컬렉션이 동일한 레일에서 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 %> 

파셜 중 하나의 예는 다음과 같이 :

여기서 반복 코드이다

<

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

나는 다른 부분에 무엇이 있는지에 달려 있다고 생각합니다. 게시 할 수 있습니까? 또한 첫 번째 샘플의 모든 코드가 단일보기에서 나온 경우 동일한 @@ contacts가 세 번 표시되는 것처럼 보일 것입니다. –

+0

우리는 비교를 위해 다른 부분도 볼 수 있습니까? 또한,보기 스크립트에서'.find '를 사용하면 MVC 원칙에 맞지 않게됩니다. –

+0

.find를 컨트롤러 또는 모델에 부착 할 수 있습니까? 그것을 생각하고 있었지만 먼저 쉽게 테스트하고 싶었습니다. – Angela

답변

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을 작성하십시오. 부분적으로 비트를 사용하여 마술을 보내면됩니다.

partial은 "contact"(_contact.html.erb)로 이름이 바뀌었고 전자 메일, 호출 및 문자를 objt라는 변수에 차례로 할당합니다.이 변수는 부분에서 액세스 할 수 있습니다. 이에 대신 직접 skip_email_url 메소드를 호출, 우리는 첫 번째 매개 변수에라는 이름의 메소드를 호출하여 "보내기"를 사용하는 방법,

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

공지 사항 (: 부분 (_contact.html.erb)에 대한

case, url_method, 호출 뷰에서 전달 된 로컬), 그리고이 메소드에 추가 매개 변수 (이 경우에는 contact 및 objt)를 전달합니다. url_method에 명명 된 메서드가 효과적으로 두 개의 매개 변수를 사용하는지 확인하십시오.

+0

어떻게해야합니까? 내가 skip-contact_email_url 작업을 만든 이유는 설정 경로가 특정 컨트롤러를 호출하기 때문입니다. – Angela

+0

가장 좋은 해결책은 skip_controller => : 지역 사용자의 이메일을 보낸 다음 url_for (: controller => skip_controller, ...)로 해당 URL을 다시 쓰고 다른 매개 변수를 수동으로 지정하는 것입니다. 그것은 안락하지는 않지만 똑같이 작동해야합니다. 또는, locals에서 controller.send (: method_name) 및 setting : method_name => : skip_contact_email_url을 수행하여 일부 마법을 사용할 수도 있습니다. – Roadmaster

+0

부분에서 전체 URL을 참조하십시오. 또는 URL을 "쓰는"방법이 있습니다. 내 버전은 다음과 같습니다. url => skip _ # {class} _url (contact.objt)하지만 작동하지 않는 것 같습니다 ... 그렇게 할 수있는 방법이 있습니까? – Angela