2011-05-24 131 views
0

我试图在索引页上的文章旁边显示“总评论数”(即7),而不是在文章页上。想使用红宝石方法作为它可能是最直接的...?博客 - 索引页上的评论数

的意见/用品/ _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 %> 
+0

当然,您的操作方法与您在其他地方的操作方式完全相同吗? – 2011-05-24 12:24:47

+0

你的意思是'<%= blog.comments.count%>'? – Mischa 2011-05-24 13:02:48

+0

@mischa我在index.html.erb页面上试过这段代码,但它不正确?!它不应该查询数据库的答案 - 不知道'博客'对象与什么关系? – ubique 2011-05-24 13:19:03

回答

0

传递一个变量,当你打电话给你的部分:

= render "article", :display_count => true 
在部分

然后:

<% display_count ||= false %> 
<%= display_count ? blog.comments.count : '' %> 
0

的正确的做法是:

<td><%= link_to "Comment count = #{article.comments.count}", article_path(article) %> 

这将简单地在索引页上的输出中添加另一列。如果您只是想显示计数,则不必链接:

<td><%= "Comment count = #{article.comments.count}" %>