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? I do have the gem installed, so, if so, how would I integrate it into my code below? If not, what would I need to do to build in pagination?
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
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
<!-- 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. This gem is just for get a number of rows with page number and you can specify number of elements per page. For exemple page 1 will give you element 1 to 30 page 2 31 to 60 .... After you have to implement the view
You can use will_paginate without any problems. Also you can look at kaminari which is more flexible.
you can call paginate methods before redirect
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. ( 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
. Then doing will_paginate @comments
should work correctly.
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.
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.