我的应用中有以下代码可以创建表格:
<% @comments.each do |comment| %>
<td>
<%= link_to (comment.body), comment.pin %>
</td>
<td>
<%= time_ago_in_words(comment.created_at) %> ago</br>
</td>
<td>
<%= link_to (comment.pin.user.name), comment.pin.user %>
</td>
<td>
<%= comment.pin.album %>
</td>
<td>
<%= comment.pin.artist %>
</td>
这是我的评论控制员:
def create
@pin = Pin.find(params[:pin_id])
@comment = @pin.comments.create(params[:comment])
@comment.user = current_user
respond_to do |format|
if @comment.save
format.html { redirect_to @pin, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
端
它很有效,除了它将每个新注释添加到表格的底部。如何以相反的顺序显示它,以便最新的评论首先出现?另外,我怎么能将它限制在最近的50条评论中呢?
答案 0 :(得分:0)
我猜你在你的控制器动作中你正在做这样的事情:
@comments = @post.comments
这会以默认顺序加载所有帖子(或随机,具体取决于数据库)。将上述内容调整为以下内容:
@comments = @post.comments.order('created_at DESC').limit(50)
您还可以为订单创建范围,并限制干预。