试图为我的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
endindex.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更新的应用程序助手
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发布于 2014-04-26 10:51:09
助手文件名应该是:
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>希望它有帮助:)
发布于 2014-04-26 10:53:28
将这些行放在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发布于 2014-04-26 12:28:12
相同的答案,但需要更改一些语法
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
https://stackoverflow.com/questions/23309742
复制相似问题