尝试将简单的库存控制系统添加到我的rails应用程序中 在现有产品数据库中添加了库存字段
创建了一个名为print_stock
的辅助方法,并将其添加到Products#index
文件中
但是当我运行应用程序时
NoMethodError in Products#index, undefined method `print_stock' for #<#Class:0x007f821b670530>:0x007f821e170898>
向产品添加库存
class AddStockToProducts < ActiveRecord::Migration
def change
add_column :products, :stock, :integer, default: 0
end
end
的index.html
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= image_tag "products/#{product.image_url}" %></td>
<td><%= product.title %></td>
<td><%= print_price(product.price) %></td>
**<td><%= print_stock(product.stock) %></td>**<---error
<td><%= product.description %></td>
<td><%= link_to 'Show', product_path(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>
<br>
print.stock.rb
def print_stock(stock)
if stock > 0
<span class="in_stock">In Stock (XX)</span>
else
<span class="out_stock">Out of Stock</span>
end
end
更新了applicationhelper
module ApplicationHelper
def print_stock(stock)
if stock > 0
"<span class="in_stock">In Stock (XX)</span>"
else
"<span class="out_stock">Out of Stock</span>"
end
end
end
/Users/neilpatel/Desktop/Rails/merchant/app/helpers/application_helper.rb:4: syntax error, unexpected tIDENTIFIER, expecting keyword_end "<span class="in_stock">In Stock (XX)</span>" ^ /Users/neilpatel/Desktop/Rails/merchant/app/helpers/application_helper.rb:6: syntax error, unexpected tIDENTIFIER, expecting keyword_end "<span class="out_stock">Out of Stock</span>" ^
更新了products_helper
module ProductsHelper
def print_price(price)
format("£%.2f",price)
end
def print_stock(stock)
if stock.to_i > 0
return true
else
return false
end
end
end
答案 0 :(得分:0)
帮助文件名应为:
products_helper.rb
,内容应为:
module ProductsHelper
def print_stock(stock)
if stock.to_i > 0
return true
else
return false
end
end
end
然后在视图中:
index.html
<table>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= image_tag "products/#{product.image_url}" %></td>
<td><%= product.title %></td>
<td><%= print_price(product.price) %></td>
<td>
<% if print_stock(product.stock) %>
<span class="in_stock">In Stock (XX)</span>
<% else %>
<span class="out_stock">Out of Stock</span>
<% end %>
</td>
<td><%= product.description %></td>
<td><%= link_to 'Show', product_path(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>
<br>
希望有所帮助:)
答案 1 :(得分:0)
将这些行放在 application_helper.rb
中module ApplicationHelper
def print_stock(stock)
if stock > 0
"<span class="in_stock">In Stock (XX)</span>"
else
"<span class="out_stock">Out of Stock</span>"
end
end
end
答案 2 :(得分:0)
相同的答案,但需要更改一些语法
module ApplicationHelper
def print_stock(stock)
if stock > 0
"<span class='in_stock'>In Stock (XX)</span>"
else
"<span class='out_stock'>Out of Stock</span>"
end
end
端
需要更改为single quotes