Forum: Ruby on Rails undefined method `<<' error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5f7640423086ad4160e59655c80dec34?d=identicon&s=25 ga rg (garg)
on 2005-11-26 01:19
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 :(

Thanks!
5f7640423086ad4160e59655c80dec34?d=identicon&s=25 ga rg (garg)
on 2005-11-26 02:50
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?
51f10739fd903d3b57561a15e4598954?d=identicon&s=25 heavysixer (Guest)
on 2005-11-26 03:32
(Received via mailing list)
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 <khurrum1@gmail.com> wrote:
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
5f7640423086ad4160e59655c80dec34?d=identicon&s=25 ga rg (garg)
on 2005-11-26 03:53
Thanks for the reply :)

I tried it again with the code from the book that you posted but
unfortunately I get the same error :( (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. :(

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
5f7640423086ad4160e59655c80dec34?d=identicon&s=25 ga rg (garg)
on 2005-11-26 04:25
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?
51f10739fd903d3b57561a15e4598954?d=identicon&s=25 heavysixer (Guest)
on 2005-11-26 07:50
(Received via mailing list)
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 <khurrum1@gmail.com> wrote:
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
5f7640423086ad4160e59655c80dec34?d=identicon&s=25 ga rg (garg)
on 2005-11-26 17:12
Thanks for the reply :)

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 :(

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

Thanks for the help :)



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 <khurrum1@gmail.com> wrote:
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
This topic is locked and can not be replied to.