2015-09-27 71 views
0

我有兩個部分有相似的輸出,但主要變化。Rails - 幹出類似的意見

  • 部分1

    <h2 class="text-center">Current Incident</h2> 
    <% active_incidents.each do |i| %> 
    <div class="incident-block"> 
        <p class="incident-date"><span class="inner-date"><%= i.updated_at.strftime('%e %b, %Y') %></span></p> 
    
        <span class="incident-name text-<%= i.convert_to_level %>"><%= i.name %> 
        <% if signed_in? %><a href="/incidents/deactivate/<%= i.id %>"> 
        <button class="btn btn-success deactivate-button btn-xs">Deactivate incident</button> 
        </a> 
        <% end %></span> 
        <span class="incident-component text-<%= i.convert_to_level %>"><%= i.component %></span> 
        <% all_events(i).each do |e| %> 
         <hr class="event-hr"/> 
         <p class="incident-description"><b><%= e.status %></b>- &nbsp;<%= e.message %></p> 
    
         <p class="incident-updated-at"><%= e.updated_at.strftime('%b %e, %H:%M %Z') %></p> 
        <% end %> 
    
        <% if signed_in? %> 
         <a href="/incidents/<%= i.id %>"> 
         <button class="btn btn-warning btn-sm">Update Incident</button> 
         </a> 
         <a href="/incidents/delete/<%= i.id %>" data-confirm="Are you sure? Deleting an incident is irreversible."> 
         <button class="btn btn-danger btn-sm">Delete Incident</button> 
         </a> 
        <% end %> 
    </div> 
    <% end %> 
    
  • 部分2

    <% if inactive_incidents.any? %><h2 class="text-center">Past Incidents</h2><% end %> 
    <% inactive_incidents.each do |i| %> 
        <div class="incident-block"> 
        <p class="incident-date"><span class="inner-date"><%= i.updated_at.strftime('%e %b, %Y') %></span></p> 
        <span class="incident-name text-<%= i.convert_to_level %>"><%= i.name %></span><span class="incident-component text-<%= i.convert_to_level %>"><%= i.component %></span> 
        <% all_events(i).each do |e| %> 
         <hr class="event-hr"/> 
         <p class="incident-description"><b><%= e.status %></b>- &nbsp;<%= e.message %></p> 
    
         <p class="incident-updated-at"><%= e.updated_at.strftime('%b %e, %H:%M %Z') %></p> 
        <% end %> 
    
        <% if signed_in? %> 
         <a href="/incidents/<%= i.id %>"> 
         <button class="btn btn-warning btn-sm">Update Incident</button> 
         </a> 
         <a href="/incidents/delete/<%= i.id %>" data-confirm="Are you sure? Deleting an incident is irreversible."> 
         <button class="btn btn-danger btn-sm">Delete Incident</button> 
         </a> 
        <% end %> 
    </div> 
    <% end %> 
    

現在,這些諧音家類似的代碼,但與渲染不同的輸出顯著的變化。由於代碼可維護性現在成爲我的問題(我必須做兩次所有更改)。我如何幹掉代碼但仍保持不同的邏輯?

+0

萬一有人需要更多的情況下,[這裏的源(https://github.com/ur0/statusify)。 –

回答

0

對於每一個,創建一個模板。然後把你的通用代碼放在中間,並把它放在一個部分。從每個模板中調用partial並將事件作爲局部變量發送。像這樣在模板中:

<%= render partial: 'incidents', locals: { incidents: inactive_incidents } %> 

你甚至可以稱之爲局部使用集合中的每個事件。

此外,如果您在公共部分內部的邏輯不同,則可以使用幫助器根據傳遞的局部變量或其他來切換該邏輯。利用這一點,也許content_tag應該有所幫助。

我建議您閱讀指南:http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials