我无法正确显示我的视图。我现在有这个作为我的代码:如何显示父母和孩子与将分页的视图?
<% for store in @stores %>
<% store.name %>
<% @stores.products.each do |p| %>
<% p.name %>
<% end %>
<% end %>
def index
@stores = Store.paginate(:page => params[:page], :per_page => 20)
end
,但最终出现错误:
undefined method `products'
我试图表现出一家商店,那么其所有的产品,在相同的重复这页面尽可能例如:
Store1
Product1
Product2
Store2
Product1
Product2
Product3
Product4
我该怎么做?
那么为什么它会给我错误:当我这样做时,未定义的局部变量或方法存储?也许是因为它在不同的控制器中,那么'StoresController'? – LearningRoR 2012-04-09 20:57:37
@Railslearner如果这是来自不同的控制器,那么'@ stores'还没有在您当前调用的方法中定义来呈现此视图。 – 2012-04-09 21:06:17
@Railslearner等,你的意思是你把'@ stores'改成'stores',现在你得到错误了吗?注意我上面的例子是'store.products.each',而不是复数'stores.product.each'。 – 2012-04-09 21:07:03