簡體   English   中英

will_paginate評論部分不渲染?

[英]will_paginate comments partial not rendering?

我按照維基上有關該gem的說明進行操作:

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

它對我的列表控制器非常有用:/我能夠使它們分頁,但是我不知道如何在評論中使用它。 我的評論控制器沒有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

這是我在ListingsController中擁有的show方法:

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代碼的第一部分實際上是呈現表單的,但是我不了解集合部分的工作方式或作用或如何將分頁應用於此。

清單/評論文件如下所示:

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

我已經嘗試了很多事情,但是我仍然沒有真正知道代碼中的更改內容。 我進入列表/評論並嘗試添加:

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

編輯:will_paginate維基說,當您收到我得到的錯誤時,您要通過一個分頁的數組,但我不知道這樣做>。>; 它並沒有顯示您將如何做...

編輯: 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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM