0
我有一對非常相似的視圖,呈現幾乎相同的信息,只有在一個視圖中有兩個額外的列,另一個行則鏈接稍微不同的嵌套資源。我最初的做法是通過使用局部視圖來保留它,然後在整個視圖中放置條件。所得的部分看起來是這樣的:Rails:DRYing類似但不同的視圖
<div id='overview_table'>
<div id="overview_header">
<span id="sort_title" class="title cell">Title<span id="publication_sort_arrow"> ↓</span></span>
<span id="sort_author" class="author cell">Author</span>
<span id="sort_status" class="status cell">Status</span>
<% if @user.present? %>
<span id="sort_impression_date" class="date cell">Date</span>
<span id="sort_impression_vote" class="votes cell">Votes</span>
<span id="sort_children_total" class="children_total cell">Replies</span>
<% end %>
</div>
<span id="sort_method">title ASC</span>
<% @publications.each do |publication| %>
<div class='<%= cycle("odd", "even") %>'>
<% if @user.present? %>
<% link = [@user, publication] %>
<% else %>
<% link = [@group, publication] %>
<% end %>
<%= link_to(link, :remote => true) do %>
<span class="title cell"><%= publication.full_title %></span>
<span class="author cell"><%= publication.authors %></span>
<span class="status cell"><%= publication_status(publication.status) %></span>
<% if @user.present? %>
<span class="date cell"><% if publication.impression_date %><%= publication.impression_date.strftime("%B %d, %Y") %><% end %></span>
<span class="votes cell"><% if publication.impression_vote %><%= publication.impression_vote.to_i %><% end %></span>
<span class="children_total cell"><% if publication.impression_vote %><%= publication.children_total %><% end %></span>
<% end %>
<% end %>
</div>
<% end %>
它工作得很好,但代碼感受到哈克。我最終將這些分離出來,分成兩種不同的觀點,但現在有很多重複的代碼。兩種方法都感覺不足。有沒有我不考慮的另一種方法?
感謝您的回答。猜猜我不應該感到驚訝,沒有一些神奇的方法來做到這一點,但我必須確定。 – nullnullnull