【发布时间】:2018-04-21 04:13:03
【问题描述】:
我是为一堂课写的:
<% if @course.comments.exists? %>
<h2>Ratings</h2>
<% $total = 0 %>
<% @course.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Score:</strong>
<%= comment.score %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.comment %>
</p>
<p>
<strong>Words in Comment:</strong>
<%= comment.comment.length %>
</p>
<% $total += 1%>
<%= link_to 'Destroy Rating', [comment.course, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
<% end %>
<% else %>
<h2>No ratings available</h2>
<% end %>
<p>
<strong>Total Comments</strong>
<%= print $total %>
</p>
我尝试过 puts 和 print,但我的屏幕上没有显示任何内容。我需要让它计算<% @course.comments.each do |comment| %> 行的每次迭代。 Ruby 不是我的强项,老师也不清楚如何实现这个目标。我对替代品持开放态度。
【问题讨论】:
标签: html ruby-on-rails ruby variables counter