gpt4 book ai didi

ruby-on-rails - 如何在 Rails View 中呈现所有评论?

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:57 26 4
gpt4 key购买 nike

我是 Rails 的新手,所以不要着急。我创建了一个博客。我已成功实现评论并将其附加到每个帖子。现在...我想在边栏中显示所有帖子的最新评论列表。我认为这里涉及两件事,更新 comment_controller.rb,然后从实际页面调用。这是评论 Controller 代码。

class CommentsController < ApplicationController

def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])

respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end

最佳答案

如果你想以最近的顺序显示任何帖子的所有评论,你可以这样做:

@comments = Comment.find(:all, :order => 'created_at DESC', :limit => 10)

在 View 中你可以做:

<% @comments.each do |comment| -%>
<p>
<%= comment.text %> on the post <%= comment.post.title %>
</p>
<% end -%>

关于ruby-on-rails - 如何在 Rails View 中呈现所有评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1781322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com