尝试构建食品杂货应用程序并需要在输入库存明细后显示库存
使用product_helper
def print_stock(stock)
if stock > 0
content_tag(:span, "In Stock (##) ", class: "in_stock")
else
content_tag(:span, "Out of Stock", class: "out_stock")
end
end
所以我要在(##)的位置显示库存。目前,当我运行时,索引页中显示有库存(##)或无库存
index.html.erb
<h1>All products</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Stock</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= product.price %></td>
<td><%= print_stock(product.stock) %></td> <- displays stock info)
<td><%= product.description %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
因此,您只需要使用字符串插值,如下所示:
content_tag(:span, "In Stock (#{stock})", class: "in_stock")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句