简体   繁体   English

will_paginate评论部分不渲染?

[英]will_paginate comments partial not rendering?

I was following the instructions on the wiki for this gem: 我按照维基上有关该gem的说明进行操作:

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

And it works great for my listings controller :/ I was able to get them paginate but I have no idea how to get it work with my comments. 它对我的列表控制器非常有用:/我能够使它们分页,但是我不知道如何在评论中使用它。 My comments controller doesn't have a show method. 我的评论控制器没有show方法。

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

This is the show method I have inside my ListingsController: 这是我在ListingsController中拥有的show方法:

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

It's what sets the comments to my understanding 是什么让评论引起了我的理解

This is the listings/show.html.erb file's part where it renders the comments: 这是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>

I know the first part of ruby code actually renders the forms but I don't understand how or what the collection part works or how to apply the pagination to this. 我知道ruby代码的第一部分实际上是呈现表单的,但是我不了解集合部分的工作方式或作用或如何将分页应用于此。

The listings/comment file looks like this: 清单/评论文件如下所示:

<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>

I've tried quite a few things but I still don't really get where or what to change in my code. 我已经尝试了很多事情,但是我仍然没有真正知道代码中的更改内容。 I went into the listing/comment and tried adding: 我进入列表/评论并尝试添加:

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

EDIT: The will_paginate wiki says you to pass a paginated array when you get the error I got, but I don't know that is done >.>; 编辑:will_paginate维基说,当您收到我得到的错误时,您要通过一个分页的数组,但我不知道这样做>。>; it doesn't show how you would do that... 它并没有显示您将如何做...

EDIT: http://csnipp.com/s/709 this website said all you had to do was create a file config/initializers/will_paginate.rb and place this line of code in it: 编辑: http : //csnipp.com/s/709,该网站说,您要做的就是创建一个文件config / initializers / will_paginate.rb并将此行代码放入其中:

 require 'will_paginate/array'

But that didn't help 但这没有帮助

EDIT: 编辑:

If you guys need to see more of the code it's actually on github: https://github.com/ilovemysillybanana/pastie/ 如果你们需要查看更多代码,则实际上在github上: https : //github.com/ilovemysillybanana/pastie/

I'm really stuck on this D: I followed a tutorial on how to make the comments but theirs didn't paginate. 我真的停留在这个D上:我遵循了有关如何发表评论的教程,但是他们没有分页。

EDIT: I fixed it! 编辑:我解决了!

The way to do it was by replacing this line: 方法是替换以下行:

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

with this line: 用这一行:

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

But for some reason I don't get the numbers to change the comment pages :/ 但是由于某种原因,我没有数字可以更改评论页面:/

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

My code was correct, what I wasn't realizing is that @listings or @list_comments was creating a local variable that needed to be paginated itself. 我的代码是正确的,我没有意识到的是@listings或@list_comments正在创建需要分页的局部变量。 I was trying to find a way to paginate either in one command(which if possible I don't care, but if you know how it'd be nice to know for the future I guess) anyway, all that I needed to do to fix this was add this: 无论如何,我一直试图找到一种方法来分页(如果可能的话,我不在乎,但是如果您知道我对将来的了解会很好),那么我需要做的所有事情解决此问题是添加此:

 <%= will_paginate @listing_comments %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM