简体   繁体   中英

will_paginate comments partial not rendering?

I was following the instructions on the wiki for this 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.

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:

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:

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

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 >.>; 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:

 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/

I'm really stuck on this D: I followed a tutorial on how to make the comments but theirs didn't paginate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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