我有一对非常相似的视图,它们呈现几乎相同的信息,只有在一个视图中有几个额外的列,而在另一个视图中,行链接略有不同的嵌套资源。我最初的方法是通过使用部分保持它干燥,然后在整个视图中放置条件。生成的部分看起来像这样:
<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 %>
它工作得很好,但代码感觉很hacky。我最终将它们分成了两个不同的视图,尽管现在有很多重复的代码。两种方法都感觉不足。还有另一种我不考虑的方法吗?