HTML/嵌入 Ruby 字数统计代码录入

新手上路,请多包涵

这是一个学校课程评分系统的评论页面,我想要做到:
1.统计每条一评论的字数
2.当整个loop结束以后统计出所有评论的字数总计

<div id="header">
<h1>Course Rating Information </h1>
<p>
Number:
<%= @course.number %>
</p>
<p>
Year:
<%= @course.year %>
</p>
<p>
Name:
<%= @course.name %>
</p>
</div>
<div id="pagebody">
<% if @course.comments.exists? %>
<h2>Ratings</h2>
<% @course.comments.each do |comment| %>

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>
<p>
  <strong>Comment:</strong>
  <%= comment.comment %>
</p>

<% end %>
<% else %>
<h2>No ratings available</h2>
<% end %>
</div>
<div id="footer">
<%= link_to 'New Comment', new_course_comment_path(@course) %>

<%= link_to 'Back to course listing', courses_path %>
</div>

阅读 2.8k
1 个回答

@course.comments.sum{ |cc| cc.comment.length }

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题