Rails 4.0:显示库存

时间:2014-07-16 10:49:04

标签: ruby-on-rails

尝试构建杂货店应用程序并需要在库存详细信息后显示库存 已进入

使用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>

1 个答案:

答案 0 :(得分:1)

所以你只需要使用字符串插值,如下所示:

content_tag(:span, "In Stock (#{stock})", class: "in_stock")