0
我有一个包含产品和类别的Ruby应用程序。Ruby数组仅存储最后一个值,不是所有值
我想要一个类别索引页面,显示每个类别的1张产品照片。
现在,它显示最后所有类别的产品图片。 (所以,有16个大类,它显示的产品照片上的每个类别16类)
我想我需要修复我如何存储或致电产品阵列。我怎么做?
码 -
在categories_controller.rbdef index
@categories = Category.all.order('name ASC')
@products = []
@categories.each do |category|
@products = Product.where(category_id: category.id).take(1)
end
end
在类别
/index.html.erb
<% @categories.each do |category| %>
<% @products.each do |product| %>
<%= link_to image_tag(product.image), category %>
<% end %>
<%= link_to category.name, category %>
<%= category.description %>
谢谢!我在控制器文件中将=更改为<<。加载类别索引页时,现在我得到未定义的方法'图像'错误。我应该对索引代码进行进一步更改吗? – supertext
你的'Product'模型中有'image'字段吗?请解释与'image'相关的数据结构。 – Yang
是的,产品型号包含图像。该图像显示正确,当它被调用数组中的最后一项。但是,只要我更改为<<,那么图像在索引文件中成为该调用的错误。产品数据库中没有空图像值或任何其他空值。 – supertext