Blog-索引页上的评论计数

| 我正在尝试在索引页面而不是文章页面上的文章旁边显示一个“总评论”数(即7)。是否想为此使用ruby方法,因为它可能是最直接的方法...? views / articles / _article.html.erb
<div class=\"article_header\">
<b>Title: </b> <%= truncate(article.title, :length => 50) %> 
by <%= article.user.username %> on <%= article.created_at.strftime(\"%d %B, %Y\") %> 
<b>Detail:</b> <%= truncate(article.body, :length => 225) %>
</div>
<br />
<%= blog.comments.count %>


<%= link_to \'Read\', article %>
 <% if can? :update, article %>
 | <%= link_to \'Edit\', edit_article_path(article) %> |      

<% end %>
    
已邀请:
调用局部变量时传递一个变量:
= render \"article\", :display_count => true
然后在您的部分:
<% display_count ||= false %>
<%= display_count ? blog.comments.count : \'\' %>
    
正确的方法是:
<td><%= link_to \"Comment count = #{article.comments.count}\", article_path(article) %>
这只会在索引页面的输出中添加另一列。如果您只想显示计数,则不必链接它:
<td><%= \"Comment count = #{article.comments.count}\" %>
    

要回复问题请先登录注册