Ruby on rails Rails 4.0:显示库存

Ruby on rails Rails 4.0:显示库存,ruby-on-rails,Ruby On Rails,正在尝试构建一个杂货店应用程序,需要在库存详细信息后显示库存 已输入 使用产品辅助工具 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 所以我想显示股票的(###)位置

正在尝试构建一个杂货店应用程序,需要在库存详细信息后显示库存 已输入

使用产品辅助工具

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")