ユーザーがプロジェクトを作成し、これらのプロジェクトにコメントできるアプリケーションを作成しました。現在、各プロジェクト ページにユーザーがコメントできるようになっています。
質問: 以下のコードに基づいて、will_paginate を使用できますか? 私は宝石をインストールしています。もしそうなら、以下のコードにどのように統合しますか? そうでない場合、ページネーションを組み込むために何をする必要がありますか?
コメント.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 -->