undefined local variable or method `line_items' for #<Cart:0x000000000ce78a18>
Extracted source (around line #3):
1 class Cart < ApplicationRecord
2 def add_product(product)
3 current_item = line_items.find_by(product_id: product.id)
4 if current_item
5 current_item.quantity += 1
6 else
app/controllers/line_items_controller.rb:27:in `create'
25 def create
26 product = Product.find(params[:product_id])
27 @line_item = @cart.add_product(product)
28
29 respond_to do |format|
30 if @line_item.save
我用Rails 6运行了一个敏捷Web开发的例子Sam Ruby,David Bryant Copeland和Dave Thomas,见第134页(第10章(。任务E:A Smarter Cart•134(,并遇到此问题。我想知道是否有人也遇到过同样的问题,它是如何解决的。。。?如有任何意见,我们将不胜感激。
我认为@rmlockerd认为line_items
应该是一个实例变量,或者line_items
也可以是LineItem
类
1 class Cart < ApplicationRecord
2 def add_product(product)
3 current_item = LineItem.find_by(product_id: product.id)
4 if current_item.present?
5 current_item.quantity += 1
6 else