2014-07-16 53 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