gpt4 book ai didi

ruby-on-rails - 如何使用获取的记录进行编号?

转载 作者:行者123 更新时间:2023-12-02 22:15:47 24 4
gpt4 key购买 nike

此代码获取 10 条最新记录

@comments = @topic.comment_threads.order("created_at DESC").page(params[:page]).per(10)

我想对获取的记录进行编号。
因为是 desc 顺序,所以会是这样的

"Displaying comments 1 - 10 of 18 in total 

No. 18 Hi
No. 17 Hello
No. 16 Hola
No. 15 Howdy
No. 14 What's up
No. 13 Hi there
No. 12 Yo
No. 11 Oh
No. 10 Ah
No. 9 Eeeee

我怎样才能在 View 中得到这个编号?应该是这样的

<%= "No. " + ????????????? %>

最佳答案

使用each_with_index

@comments.each_with_index.map {|comment, index| "No #{index} #{comment}"}

关于ruby-on-rails - 如何使用获取的记录进行编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496285/

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