gpt4 book ai didi

ruby-on-rails - 将应用程序从随机显示一个报价切换为网格中的所有报价

转载 作者:行者123 更新时间:2023-12-04 03:43:26 24 4
gpt4 key购买 nike

我之前构建了一个应用程序(我的第一个 Rails 应用程序),旨在在屏幕上随机显示数据库中的单引号(每次刷新时显示不同的单引号)。我现在的目标是重构该应用程序,以网格格式显示所有引号(我猜是分页的)。

问题是我不确定如何提取所有引号显示在页面上,而不是随机显示一个。在 Controller 中,我以前有:

def index
@quote = Quote.order("RANDOM()").first
end

会是这样吗?

def index
Quote.each do
@quote = Quote.order("RANDOM()")
end
end

最佳答案

在 Controller 中输入:

def index
@quotes = Quote.order("RAND()").all
end

在您的 View 中输入:

<% @quotes.each do |quote| %>
<%= quote.id
<%= quote.X <---------REPLACE X for the column to show
<%= quote.X <---------REPLACE X for the column to show
<% end%>

关于ruby-on-rails - 将应用程序从随机显示一个报价切换为网格中的所有报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402903/

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