#99 Complex Partials

How do you handle partials which have differences depending on the action which is rendering them? Here's three suggestions for this problem.
<!-- index.html.erb -->
<% for article in @articles %>
<% render :layout => 'article', :object => article do %>
<p><%=h article.description %></p>
<p><%= link_to "Read More...", article %></p>
<% end %>
<% end %>

<!-- show.html.erb -->
<% stylesheet 'article' %>
<% render :layout => 'article', :object => @article do %>
<%= simple_format(h(@article.content)) %>
<% end %>

<!-- _article.html.erb -->
<div class="article">
<h2><%= link_to_unless_current h(article.name), article %></h2>
<div class="info">
by <%= article.author %> on <%= article.created_at.to_s(:long) %>
<span class="comments">
<%= link_to pluralize(article.comments.size, 'Comment'), article %>
</span>
</div>
<div class="content">
<%= yield %>
</div>
</div>

<!-- layouts/application.html.erb -->
<%= yield(:head) %>

# application_helper.rb
def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args) }
end

/* article.css */
.article .comments_link {
display: none;
}
發佈了108 篇原創文章 · 獲贊 0 · 訪問量 2931
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章