current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
ich einen Fehler (3.) Linie bekam, sagt er ** undefined Methode` +' für nil: NilClass Ich habe keine Ahnung Was ist los: | **nicht definierte Methode `+ 'für nil: NilClass (current_item.quantity + = 1)
class Cart < ApplicationRecord
has_many :line_items, dependent: :destroy
def add_product(product_id)
current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
**And my line_controller**
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
def change
add_column :line_items, :quantity, :integer, default: 1
end
"Ich habe keine Ahnung, was los ist" - der Fehler sagt Ihnen. 'current_item.quantity' ist null. –