显示项目数量

时间:2017-07-23 02:22:13

标签: ruby-on-rails ruby

我想计算我的页面上的评论数量,名为view

这是我的控制器代码

  def view
    @gallery = Gallery.find_by!(id: params[:id]).decorate
    @comments = Comment.select(:user_id, :description).includes(:user).where(gallery_id: @gallery.id)
    if user_signed_in?
      @like = current_user.likes.where(gallery_id: @gallery.id).first
    end
  end

这是查看页面

  .text-container
    p: strong Komentar
    - @comments.each do |comment|
      .media.testimoni-box
        .col-md-12.jaminan
          .media-heading.strong = comment.user_personal_name
          = comment.description
          - # want to show the number of comment here

请帮帮我。并感谢提前

1 个答案:

答案 0 :(得分:1)

您可以循环@comments集合with_index并将其用作计数器

.text-container
  p: strong Komentar
  - @comments.each.with_index(1) do |comment, count|
    .media.testimoni-box
      .col-md-12.jaminan
        .media-heading.strong = comment.user_personal_name
        = comment.description
        = count