评论部分将最新评论排在顶部而不是底部
Comments Section Ordering The Comments Newest At Top Instead Of Bottom
几周前我部署了一个网络应用程序,用户可以在其中制作 post 其他人可以发表评论的内容。评论将按最旧的顺序排列在顶部,最新的在底部。这工作正常,直到大约 2 周前我添加了编辑 posts 的能力,从那时起评论并一直在所有新的 posts 上以相反的方式排序。即最新的在顶部。
我检查了我的提交,看不出我可以做些什么来引起改变。最糟糕的是,它仍然像我希望的那样在本地工作,这使得调试变得困难。
评论控制器:
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find_by(id: params[:micropost_id])
@comment =
@micropost.comments.create(params[:comment].permit(:name,:body))
@comment.name = current_user.name
if @comment.save
flash[:success] = "Comment Posted"
end
redirect_to request.referrer
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to request.referrer
end
end
来自 Microposts 控制器的节目:
def show
@micropost = Micropost.find_by(id: params[:id])
@post = Micropost.includes(:comments).find(params[:id])
@micropost.punch(request)
end
我尝试将 .order(:created_at =>:desc)
添加到上面的 @post 中,但没有任何区别。我做了 :asc 来测试,但评论不会重新排序。我在其他地方成功使用了.order。
在 Micropost 视图上呈现的评论视图:
<li>
<ol class="microposts">
<% @post.comments.each do |comment| %>
<li>
<article class="article-container-full">
<%= comment.name %>
<hr>
<%= simple_format(comment.body) %>
<hr>
<p class="posted-time">
Posted <%= time_ago_in_words(comment.created_at) %> ago
</p>
</article>
<br>
</li>
</ol>
<% end %>
</li>
评论表单视图
<%= form_for([@micropost, @micropost.comments.build]) do |f| %>
<br>
<h4 class="article-content">
<%= f.hidden_field :name ,value: current_user.name %>
<%= "Commenter: #{current_user.name}" %>
</h4>
<p>
<%= f.text_area :body, rows:"4" ,required: true %>
</p>
<br>
<p>
<%= f.submit class: 'submit-button' %>
</p>
<% end %>
微型post 查看评论渲染
</li>
<h1 class="article-container-header"> Comments</h1>
<article>
<%= render "comments/comment" %>
</article>
<% if logged_in? %>
<article class="article-container-full">
<%= render 'comments/form' %>
</article>
<% end %>
我试过在几个地方使用 .order(:created_at =>:desc)
或 .order(:created_at =>:asc)
但无济于事。
欢迎所有建议。
在你的展示方法中:
@post = Micropost.find(params[:id])
@comments = @post.includes(:comments).order('comments.created_at DESC')
然后您可以在 HTML 文件中迭代@comments。
您需要订购评论。
微博查看:
<% @post.comments.order(created_at: :asc).each do |comment| %>
..
<% end %>
几周前我部署了一个网络应用程序,用户可以在其中制作 post 其他人可以发表评论的内容。评论将按最旧的顺序排列在顶部,最新的在底部。这工作正常,直到大约 2 周前我添加了编辑 posts 的能力,从那时起评论并一直在所有新的 posts 上以相反的方式排序。即最新的在顶部。
我检查了我的提交,看不出我可以做些什么来引起改变。最糟糕的是,它仍然像我希望的那样在本地工作,这使得调试变得困难。
评论控制器:
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find_by(id: params[:micropost_id])
@comment =
@micropost.comments.create(params[:comment].permit(:name,:body))
@comment.name = current_user.name
if @comment.save
flash[:success] = "Comment Posted"
end
redirect_to request.referrer
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to request.referrer
end
end
来自 Microposts 控制器的节目:
def show
@micropost = Micropost.find_by(id: params[:id])
@post = Micropost.includes(:comments).find(params[:id])
@micropost.punch(request)
end
我尝试将 .order(:created_at =>:desc)
添加到上面的 @post 中,但没有任何区别。我做了 :asc 来测试,但评论不会重新排序。我在其他地方成功使用了.order。
在 Micropost 视图上呈现的评论视图:
<li>
<ol class="microposts">
<% @post.comments.each do |comment| %>
<li>
<article class="article-container-full">
<%= comment.name %>
<hr>
<%= simple_format(comment.body) %>
<hr>
<p class="posted-time">
Posted <%= time_ago_in_words(comment.created_at) %> ago
</p>
</article>
<br>
</li>
</ol>
<% end %>
</li>
评论表单视图
<%= form_for([@micropost, @micropost.comments.build]) do |f| %>
<br>
<h4 class="article-content">
<%= f.hidden_field :name ,value: current_user.name %>
<%= "Commenter: #{current_user.name}" %>
</h4>
<p>
<%= f.text_area :body, rows:"4" ,required: true %>
</p>
<br>
<p>
<%= f.submit class: 'submit-button' %>
</p>
<% end %>
微型post 查看评论渲染
</li>
<h1 class="article-container-header"> Comments</h1>
<article>
<%= render "comments/comment" %>
</article>
<% if logged_in? %>
<article class="article-container-full">
<%= render 'comments/form' %>
</article>
<% end %>
我试过在几个地方使用 .order(:created_at =>:desc)
或 .order(:created_at =>:asc)
但无济于事。
欢迎所有建议。
在你的展示方法中:
@post = Micropost.find(params[:id])
@comments = @post.includes(:comments).order('comments.created_at DESC')
然后您可以在 HTML 文件中迭代@comments。
您需要订购评论。
微博查看:
<% @post.comments.order(created_at: :asc).each do |comment| %>
..
<% end %>