small medium large xlarge

06 May 2013, 13:18
acc (2 posts)

when i empty cart, than add new product to cart, got the error message

NoMethodError in LineItemsController#create

undefined method `save’ for nil:NilClass

Extracted source (around line #43): 41 42 43 44 45 46

respond_to do |format|
    format.html { redirect_to @line_item.cart }
    format.json { render action: 'show',
      status: :created, location: @line_item }

Rails.root: /Users/xxx/rails_projects/depot

Application Trace | Framework Trace | Full Trace app/controllers/line_items_controller.rb:43:in block in create' app/controllers/line_items_controller.rb:42:in create’ Request


{“authenticity_token”=>”a7Y17MUp643PZknqtjFQOwrPAWVgwZoqYop1dZXgO4w=”, “product_id”=>”1”}

06 May 2013, 15:49
Sam Ruby (633 posts)

So… you are calling save on an object which has a value of nil.

Working backwards, it appears that @line_item is nil.

Not shown in this excerpt, but @line_item should be set as follows:

@line_item = @cart.add_product(

Is that line present? If so, does your code for add_product match models/cart.rb?

07 May 2013, 05:58
acc (2 posts)

is my fault, at models/cart.rb type current to cyrrent , thks Sam.. ^^

You must be logged in to comment