Undefined method `<<' error

Hi. I’m having a strange error. I’m following a book on Ruby on Rails
and it’s guiding me on how to make a shopping cart.

I personally have double checked all the codes but I get stuck with the
error:

NoMethodError in Store#add_to_cart

undefined method `<<’ for #LineItem:0xb6cd2e60

I have add_to_cart:

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

And:

    def add_product(product)
            item = LineItem.for_product(product)
            @items << item
            @total_price += product.price
    end

and it seems to not like @items << item

Any help is appreciated. I think I’ve checked the code and I couldn’t
find anything wrong… but i’m sure I’ve done something that I can’t
catch :frowning:

Thanks!

I just redid all the code in the chapter and I get the same thing
again. It’s as if it doesn’t understand that << means to append or
something.

Has anyone else had this problem before?

You may have a typo.
i am looking at hte hbook and it says:

@items << LineItem.for_product(product)
@total_pice += product.price

hope that helps,
Mark

On 11/25/05, Khurrum Ma [email protected] wrote:

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Thanks for the reply :slight_smile:

I tried it again with the code from the book that you posted but
unfortunately I get the same error :frowning: (sorry I posted the wrong code
above earlier. I moved it around just to see if it helps and forgot to
change it back)

undefined method `<<’ for #LineItem:0xb6ab7914

So now cart.rb is:

class Cart
attr_reader :items
attr_reader :total_price

    def initialize
            @items = []
            @total_price = 0.0
    end

    def add_product(product)
            @items << LineItem.for_product(product) # <-- error?
            @total_price += product.price
    end

end

This error is very confusing. :frowning:

heavysixer wrote:

You may have a typo.
i am looking at hte hbook and it says:

@items << LineItem.for_product(product)
@total_pice += product.price

hope that helps,
Mark

Could it have anything to do with the fact that originally the mySQL
database was myISAM and not InnoDB?

Is there any way to confirm that the contraints are being put on?

I think that your problem might be with your line item class not the
cart class.
I think the error is actually saying it is looking for the “<<” method
inside lineItem. I would check the syntax of your lineItem class
again. I am using myISAM without troubles.
Hope that helps,
Mark

On 11/25/05, Khurrum Ma [email protected] wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails

Thanks for the reply :slight_smile:

I just noticed that it was working in IE but not firefox so I deleted
the cookies that I had from my domain and it started working :frowning:

hehe a day was wasted hehe but I guess I learned something.

Thanks for the help :slight_smile:

heavysixer wrote:

I think that your problem might be with your line item class not the
cart class.
I think the error is actually saying it is looking for the “<<” method
inside lineItem. I would check the syntax of your lineItem class
again. I am using myISAM without troubles.
Hope that helps,
Mark

On 11/25/05, Khurrum Ma [email protected] wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails