简体   繁体   English

在显示视图中包括Rails Index支架

[英]Include Rails Index scaffold in show view

Sorry if this has been asked - i looked but couldnt find. 抱歉,是否有人问我-我看了但找不到。

I'm trying to put the following code into a partial: 我正在尝试将以下代码放入部分代码中:

<% @products.each do |product| %>
   <%= product.id %>'>
   <img src='<%= product.image_1 %>'/>
   <%= product.title %>
   <%= product.image_2 %>
 <% end %>

But can't make it work - can anyone help? 但是不能使它起作用-有人可以帮助吗?

Thanks. 谢谢。

When calling your partial you need to provide the @products variable 调用您的局部变量时,您需要提供@products变量

<%= render partial: 'your_partial', locals: {products: @products} %>

In your partial: 在您的部分中:

<% products.each do |product| %>
  <%= product.id %>
  <img src='<%= product.image_1 %>'/>
  <%= product.title %>
  <%= product.image_2 %>
<% end %>

A better way to do it would be just to pass the collection into the partial: 更好的方法是将集合传递给局部对象:

<%= render partial: 'your_partial', collection: @products %>

Then create the _product partial with the following code: 然后使用以下代码创建_product部分:

<%= product.id %>
<%= image_tag(product.image_1) %> 
<%= product.title %>
<%= product.image_2 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM