我创建了一个应用程序,用户可以在其中创建项目并对这些项目发表评论。现在,我可以让用户在每个项目页面上发表评论。
问题:根据我下面的代码,我可以使用 will_paginate 吗?我确实安装了 gem,所以,如果是这样,我将如何将它集成到下面的代码中?如果没有,我需要做什么来建立分页?
评论.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
评论控制器.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
项目/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 -->