我创建了一个应用程序,用户可以在其中创建项目并对这些项目发表评论。现在,我可以让用户在每个项目页面上发表评论。
问题 1:订单当前显示评论,然后在此下方显示提交新评论的选项。当我尝试重新排列它以使其在评论列表之前显示提交评论时,我得到 NoMethodError: undefined method `name' for nil:NilClass。我需要做什么才能使评论提交框位于当前评论上方。
问题 2:现在,评论被列出,最新的在底部。如何翻转订单以使最近的订单在最前面?
评论.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
<!-- Show Comments -->
<p class="comment_header">Comments:</p>
<% if @project.comments.blank? %>
<p>No comments made yet for this project.</p>
<% else %>
<% @project.comments.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 %>
<!-- 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 %>
<!-- end of comments section -->