简体   繁体   English

Ruby on Rails - Paginating Comments

[英]Ruby on Rails - Paginating Comments

I have created an application where Users can create Projects and make Comments on these Projects. 我创建了一个应用程序,用户可以在这些项目上创建项目并发表评论。 Right now, I have ability for users to make comments on each Project page. 现在,我有能力让用户在每个项目页面上发表评论。

Question: Based on my code below, will I be able to use will_paginate? 问题:根据下面的代码,我能使用will_paginate吗? I do have the gem installed, so, if so, how would I integrate it into my code below? 我确实安装了gem,所以,如果是这样,我将如何将它集成到我的代码中? If not, what would I need to do to build in pagination? 如果没有,我需要做什么来建立分页?

comment.rb comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :content, :project_id, :user_id
  validates :content, presence: true

  belongs_to :project
  belongs_to :user

  scope :newest, order("created_at desc")
end

comments_controller.rb comments_controller.rb

class CommentsController < ApplicationController
  before_filter :authenticate_user! 

  def create
    project = Project.find(params[:project_id])
    @comment = project.comments.create!(params[:comment])
    redirect_to project_path(project)
  end
end

projects/show.html.erb 项目/ show.html.erb

        <!-- Add Comments -->

          <% if signed_in? %>
            <p class="comment_header">Add Comment:</p>

            <span class="comment">
                <%= form_for([@project, @project.comments.build]) do |f| %>
                  <div class="field">
                    <%= f.text_area :content, :class => "span7", :rows => "3" %>
                  </div>

                  <%= f.hidden_field :user_id, :value => current_user.id %>

                  <div class="actions">
                    <%= f.submit "Add Comment", :class => "btn btn-header" %>
                  </div>
                <% end %>
            </span>

          <% else %>

            <p class="comment_header"><%= link_to 'Sign in', new_user_session_path %> to post comments.</p> 

          <% end %>

          <!-- Show Comments -->
          <p class="comment_header">Comments:</p>

          <% if @project.comments.blank? %>     
            <p>No comments made yet for this project.</p>        
          <% else %>        
            <% @project.comments.newest.each do |comment| %>   
              <div class="comments">        
                <p><%= comment.content %></p>
                <span>By <%= link_to comment.user.name, comment.user %> <%= time_ago_in_words(comment.created_at) %> ago</span>
              </div>
            <% end %>       
          <% end %>
          <!-- end of comments section -->

Yes you can use will_paginate gem. 是的,你可以使用will_paginate gem。 This gem is just for get a number of rows with page number and you can specify number of elements per page. 此gem仅用于获取具有页码的行数,您可以指定每页的元素数。 For exemple page 1 will give you element 1 to 30 page 2 31 to 60 .... After you have to implement the view 例如,第1页将为您提供元素1到30第2页31到60 ....您必须实现视图

You can use will_paginate without any problems. 您可以毫无问题地使用will_paginate。 Also you can look at kaminari which is more flexible. 你也可以看一下更灵活的kaminari

you can call paginate methods before redirect 你可以在重定向之前调用paginate方法

in you controller 在你的控制器

  @comments = @project.comments.pagenate(page: params[:page]||1,per_page: 20)

and add the following to you template 并将以下内容添加到您的模板中

  <%= will_paginate @comments %>

You haven't implemented will_paginate correctly. 您尚未正确实现will_paginate ( You can read about proper implementation in the documentation here. ) 您可以在此处阅读文档中的正确实现。

Simply put, you need to use something like @comments = @project.comments.paginate(page: params[:page]) in the controller for the index action and then iterate over @comments . 简而言之,您需要在控制器中使用@comments = @project.comments.paginate(page: params[:page])之类的内容来执行索引操作,然后迭代@comments Then doing will_paginate @comments should work correctly. 然后做will_paginate @comments应该正常工作。

You can't just do the query ( will_paginate @user.comments.newest ) in the view because the paginate method returns a special object that handles pagination, unlike a normal ActiveRecord query. 您不能只在视图中执行查询( will_paginate @user.comments.newest ),因为paginate方法返回一个处理分页的特殊对象,与普通的ActiveRecord查询不同。

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

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