0
你會如何重構這個邏輯填充部分?Rails部分超載邏輯需要重構
<%- for post in @posts -%>
<div class="post">
<%= link_to post.title, post %>
<%- if post.name.empty? -%>
<%- else -%>
<span class="name">
by
<%- if post.email.blank? -%>
<%= post.name %>
<%- else -%>
<a href="mailto:<%= post.email %>"><%= post.name %></a>
<%- end -%>
</span>
<%- end -%>
<span class="time">
active   <%= timeago(post.updated_at) %>
</span>
<%- if post.comments.empty? -%>
<span class="reply">
<%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
</span>
<% else %>
<span class="reply">
<%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>
</span>
<%- end -%>
<p><%= sanitize post.content,
:tags => %w(a embed img object p param),
:attributes => %w(allowfullscreen allowscriptaccess href name src type value) %></p>
<%- unless controller.controller_name == "tags" -%>
<%- unless post.tag_list.nil? || post.tag_list.empty? -%>
<%- post.tags.each do |t| -%>
<div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<%- end -%>
<%- end -%>
<%- end -%>
<%- unless post.comments.empty? -%>
<div class="comments">
<%= render :partial => post.firstcomments %>
<%- if post.comments.count >= 4 -%>
<%= link_to 'more...', :action => 'show', :id => message %>
<%- end -%>
</div>
<%- end -%>
</div>
<%- end -%>
注: post.firstcomments僅返回3個最新的帖子。 使用Rails 3和Ruby 1.9.2。 我還沒有看過代碼的清理部分,我意識到Rails 3默認情況下會將所有內容都轉義出來,所以現在可以安全地忽略它,除非有人知道如何轉換它。
我的模型很乾淨,我的控制器很體面,但這部分很糟糕。它做它需要做的事情,但它刷新頁面時佔用大部分渲染時間。意見,建議和代碼非常感謝。感謝您閱讀我的問題。