Ruby on rails 在rails中查询7个随机产品,遍历它们,并接收未定义的方法'each';字符串错误
在我的taxons\u控制器中,我有以下内容:Ruby on rails 在rails中查询7个随机产品,遍历它们,并接收未定义的方法'each';字符串错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,在我的taxons\u控制器中,我有以下内容: @taxon = Taxon.find_by_permalink(params[:id]) @related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7).name 我已经测试过一种形式,它会拉动一个随机对象,但当我把它放在我的show view中时: <ul> <%= @related_products.ea
@taxon = Taxon.find_by_permalink(params[:id])
@related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7).name
我已经测试过一种形式,它会拉动一个随机对象,但当我把它放在我的show view中时:
<ul>
<%= @related_products.each do |related_product| %>
<li><%= related_product.name %></li>
<% end %>
</ul>
我在狂欢/税务节目中得到了一个命名错误
“Spree::Product”的未定义方法“each”:字符串
提取的源(第41行附近):
41
我一直在使用。每个地方的变化,我不明白我做错了什么。谢谢 您正试图迭代一个
字符串
,而您应该迭代一个数组
# Remove the .name at the end
@related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7).name
@related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7)
啊,是的。我是个白痴。非常感谢。
# Remove the .name at the end
@related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7).name
@related_products = @taxon.products.offset(rand(Spree::Product.count)).limit(7)