我已經創建了一個應用程序,用戶可以在其中創建項目並對這些項目發表評論。現在,我有能力讓用戶在每個項目頁面上發表評論。Ruby on Rails - 分頁評論
問題:根據我的代碼,我可以使用will_paginate嗎?我確實安裝了寶石,如果是這樣,我將如何將它集成到我的代碼中?如果不是,我需要做些什麼來建立分頁?
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
項目/ 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 -->
我確實有寶石,並嘗試實施,但沒有奏效。請參閱我對以下@freemanoid的迴應。 – spl 2013-04-23 08:35:59