29 May 2013, 23:34
Generic-user-small

anthony hack (3 posts)

Hi I am new to rails and so far the book has been great and very helpful. I have run into a problem though On iteration D3 adding a button. I have updated the line_items_controller.rb with the following.

def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.line_items.build
    @line_item.product = product

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
        format.json { render json: @line_item, status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

When I click add to cart on the front page I get the following error.

undefined method `line_items’ for Cart:0x007fa71c23d168

Here are the parameters being passed.

{“authenticity_token”=>”MtwDjh3AtSRJjEsCJv49gpNPbOv8cb7Fl8FFkgzshmo=”, “product_id”=>”2”}

Not sure what is wrong as I followed the book to the letter. Unless I missed something, sometimes more eyes are better.

Thanks for any help.

29 May 2013, 22:51
Samr_small_pragsmall

Sam Ruby (584 posts)

Unfortunately, it appears that the forum software ate part of the message. Can you share what comes after the #?

Judging by the symptoms, there is a problem with the current_cart method in that it is returning an object of the wrong type.

29 May 2013, 23:39
Generic-user-small

anthony hack (3 posts)

Thank you for the quick response, I edited the post to include what was in front of the hash tag.

30 May 2013, 00:39
Samr_small_pragsmall

Sam Ruby (584 posts)

OK, so @cart is of the right type. The next thing to check is that the model is correct. Compare your code against the following:

http://media.pragprog.com/titles/rails4/code/rails40/depot_f/app/models/cart.rb

This is from iteration D2 on page 109 of the b2_0 PDF.

03 Jun 2013, 14:43
Generic-user-small

anthony hack (3 posts)

Sorry for the late reply, just had a chance to check my code. It was an error on my part in the cart.rb i had line_item with a capital L. Thanks for pointing me in the right direction though didn’t catch that the first time I looked through the code.

  You must be logged in to comment