will_paginate评论部分未渲染?

will_paginate comments partial not rendering?

我是按照 wiki 上的说明进行操作的 gem:

https://github.com/mislav/will_paginate/wiki

它对我的列表控制器非常有用:/我能够让它们分页,但我不知道如何让它与我的评论一起工作。我的评论控制器没有显示方法。

class CommentsController < ApplicationController
  before_action :authenticate_user!, :except => [:show]
  def create
    @comment = Comment.new(params[comment_params])
    @comment.listing_id = params[:listing_id]
    @comment.user_id = current_user.id
    @comment.body = params[:comment][:body]
    if @comment.save
      flash[:success] = "Comment Successful."
      redirect_to listing_path(@comment.listing)
    else
      flash[:alert] = "Comment Failed."
    end
  end
  def destroy
    @comment = @listing.comments.find(params[:id])
    @comment.destroy
    flash[:success] = "Comment Removed."
    redirect_to listing_path(@listing)
  end
  private
  def comment_params
    params.require(:comment).permit(:user, :body)
  end
end

这是我的 ListingsController 中的显示方法:

def show
   @comment = Comment.new
   @comment.listing_id = @listing_id
end

这就是我理解评论的原因

这是 listings/show.html.erb 文件中呈现评论的部分:

<div class="commentblock">
  <div class="text-left">
    <%= render partial: 'comments/form' %>
    <%= render partial: 'listings/comment',  collection: @listing.comments.reverse %>
  </div>
</div>

我知道 ruby 代码的第一部分实际上呈现了表单,但我不明白集合部分的工作原理或内容,也不了解如何将分页应用于此。

listings/comment 文件如下所示:

<div class="panel panel-default">
  <div class="panel-heading"><%= comment.user.username %>: <%= time_ago_in_words(comment.created_at)%> ago</div>
  <div class="panel-body">
    <%= comment.body %>
  </div>
</div>

我已经尝试了很多东西,但我仍然没有真正了解我的代码中需要更改的地方或内容。我进入 listing/comment 并尝试添加:

<%= will_paginate(comment) %> # this crashed, undefined method `total_pages' for #<Comment:0x007ff556a36468>

编辑:will_paginate wiki 说当你遇到我遇到的错误时要传递一个分页数组,但我不知道那已经完成 >.>;它没有显示您将如何做...

编辑: http://csnipp.com/s/709 这个网站说你所要做的就是创建一个文件 config/initializers/will_paginate.rb 并将这行代码放入其中:

 require 'will_paginate/array'

但这并没有帮助

编辑:

如果你们需要查看更多代码,它实际上在 github 上: https://github.com/ilovemysillybanana/pastie/

我真的被这个 D 困住了:我遵循了关于如何发表评论的教程,但他们没有分页。

编辑: 我修好了它!

实现它的方法是替换这一行:

<%= render partial: 'listings/comment',  collection: @listing.comments.reverse %>

这一行:

<%= render partial: 'listings/comment',  collection: @list_comments = @listing.comments.paginate(:page => 1, :per_page => 5) %>

但出于某种原因,我没有得到更改评论页面的数字:/

<%= render partial: 'listings/comment',  collection: @listings = @listing.comments.reverse.paginate(:page => params[:page], :per_page => 10) %>

我的代码是正确的,但我没有意识到@listings 或@list_comments 正在创建一个需要自己分页的局部变量。我试图找到一种在一个命令中分页的方法(如果可能的话我不在乎,但如果你知道我想知道未来会怎样)无论如何,我需要做的就是修复这个是添加这个:

 <%= will_paginate @listing_comments %>