2014-07-16 104 views
0

試圖建立一個雜貨店應用程序,並需要後股價細節 一直在使用product_helper的Rails 4.0:顯示股票

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

所以,你只需要使用字符串插值,這樣:

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

嗨,現在顯示股票,謝謝 – Neil