# NoMethodError in Store#add\_to\_cart

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855>\
**Category:** Rails\
**Created:** [November 25, 2005, 5:35pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855 "2005-11-25T17:35:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 25, 2005, 5:35pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/1 "2005-11-25T17:35:44Z")

</div>

Dear all,

I’m Going, by the book, through the book “Agile Web D. with  
Rails”.  
(I hope btw that it’s not off topic to share my mental bump here. )

I’m running into the following error:

NoMethodError in Store#add\_to\_cart

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.product\_id

Have checked all the syntaxes several times and I’m stuck on pages  
85-86.

Can somebody help a Rails newby out, where to start?

Thanx a million.

Kind reagrds,

Gerard.

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 25, 2005, 8:37pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/2 "2005-11-25T20:37:34Z")

</div>

On 25 Nov 2005, at 16:32, Gerard wrote:

> I’m running into the following error:
> 
> NoMethodError in Store#add\_to\_cart
> 
> You have a nil object when you didn’t expect it!  
> The error occured while evaluating nil.product\_id
> 
> Can somebody help a Rails newby out, where to start?

Post your method (add\_to\_cart) body please. I’m guessing that it’s  
something to do with you instanciating the LineItem object but can’t  
tell for sure.

## Yours, Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
[Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 11:28am UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/3 "2005-11-26T11:28:03Z")

</div>

Craig,

First, thanx for the response. Below is the add\_to\_cart action.

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

Funny enough it now comes with an different error (a step further since  
it’s  
redirected there), even though no code was changed in the mean time,  
saying:

NoMethodError in Store#display\_cart

Showing app/views/store/display\_cart.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.product

Extracted source (around line #6):

3:

  
4: \<%  
5: for item in @items  
6: product = item.product  
7: -%\>  
8:   
9: 

I can’t help to think that an initial value is not set somewhere, since  
it  
keep talking about a nil object.

Looking forward to your response.

Kind Regards,

Gerard.

On Friday 25 November 2005 20:35, Craig W. tried to type something  
like:

> Post your method (add\_to\_cart) body please. I’m guessing that it’s
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
$biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
$fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
~  
~  
:wq!

| \<%= item.quantity %\> |

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 12:52pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/4 "2005-11-26T12:52:44Z")

</div>

On 26 Nov 2005, at 10:24, Gerard J. Petersen wrote:

> Funny enough it now comes with an different error (a step further  
> since it’s  
> redirected there), even though no code was changed in the mean  
> time, saying:
> 
> NoMethodError in Store#display\_cart

Could you post your display\_cart method here? The change you mention  
seems to imply that the add\_to\_cart method is working now so we  
should concentrate our efforts towards the code that makes the app cry.

## Yours, Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
[Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 1:25pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/5 "2005-11-26T13:25:09Z")

</div>

It sounds like @items is not getting set. Check your display\_cart  
method in your store\_controlled.rb. It should be something like:

```
     def display_cart
             @items=@cart.items
             if @items.empty?
                     redirect_to_index 'Your cart is empty'
             end
             if params[:context]==:checkout
                     render :layout => false
             end
     end

```

If you are still stuck, put in some debugging lines to see what’s  
going on, e.g.

```
     def display_cart
             @items=@cart.items
	p @cart.inspect
	p @items.inspect

```

and see what’s displayed in Webrick’s log.

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 2:49pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/6 "2005-11-26T14:49:24Z")

</div>

Derek,

This is the display\_cart method.

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

It feels like an initial setting is missing. In  
app/controllers/application.rb  
The following is set.

class ApplicationController \< ActionController::Base  
model :cart  
model :line\_item  
end

I’ll try your debugging suggestions and get back to you. BTW, what does  
the  
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type  
something  
like:

> ```
> end
> 
> ```
> 
> and see what’s displayed in Webrick’s log.
> 
> > ```
> > redirect_to(:action => 'display_cart')
> > 
> > ```
> > 
> > 8:
> > 
> > > > You have a nil object when you didn’t expect it!  
> > > > –  
> > > > –  
> > > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > > > –  
> > > > $biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
> > > > $fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
> > > > ~  
> > > > ~  
> > > > :wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:21pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/7 "2005-11-26T15:21:37Z")

</div>

Craig,

First, thanx for the response. Below is the add\_to\_cart action.

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

Funny enough it now comes with an different error (a step further since  
it’s  
redirected there), even though no code was changed in the mean time,  
saying:

NoMethodError in Store#display\_cart

Showing app/views/store/display\_cart.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.product

Extracted source (around line #6):

3:

  
4: \<%  
5: for item in @items  
6: product = item.product  
7: -%\>  
8:   
9: 

I can’t help to think that an initial value is not set somewhere, since  
it  
keep talking about a nil object.

Looking forward to your response.

Kind Regards,

Gerard.

On Friday 25 November 2005 20:35, Craig W. tried to type something  
like:

> Post your method (add\_to\_cart) body please. I’m guessing that it’s
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

| \<%= item.quantity %\> |

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:21pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/8 "2005-11-26T15:21:38Z")

</div>

Craig,

Below the display\_cart method:

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

No to exiting IMHO.

Thanx

Regards,

Gerard.

On Saturday 26 November 2005 12:49, Craig W. tried to type  
something  
like:

> should concentrate our efforts towards the code that makes the app cry.
> 
> ## Yours, Craig
> 
> Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
> [Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

–  
$biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
$fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
~  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:21pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/9 "2005-11-26T15:21:38Z")

</div>

Derek,

This is the display\_cart method.

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

It feels like an initial setting is missing. In  
app/controllers/application.rb  
The following is set.

class ApplicationController \< ActionController::Base  
model :cart  
model :line\_item  
end

I’ll try your debugging suggestions and get back to you. BTW, what does  
the  
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type  
something  
like:

> ```
> end
> 
> ```
> 
> and see what’s displayed in Webrick’s log.
> 
> > ```
> > redirect_to(:action => 'display_cart')
> > 
> > ```
> > 
> > 8:
> > 
> > > > You have a nil object when you didn’t expect it!  
> > > > –  
> > > > –  
> > > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > > > –  
> > > > $biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
> > > > $fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
> > > > ~  
> > > > ~  
> > > > :wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:25pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/10 "2005-11-26T15:25:40Z")

</div>

On 26 Nov 2005, at 13:43, Gerard J. Petersen wrote:

> Below the display\_cart method:
> 
> def display\_cart  
> @cart = find\_cart  
> @items = @cart.items  
> end

Does your Cart model initialise the items attribute?

## Yours, Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
[Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 2:45pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/11 "2005-11-26T14:45:23Z")

</div>

Craig,

Below the display\_cart method:

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

No to exiting IMHO.

Thanx

Regards,

Gerard.

On Saturday 26 November 2005 12:49, Craig W. tried to type  
something  
like:

> should concentrate our efforts towards the code that makes the app cry.
> 
> ## Yours, Craig
> 
> Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
> [Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

–  
$biz = [http://www.gp-net.nl](http://www.gp-net.nl) ;  
$fun = [http://www.mrmental.com](http://www.mrmental.com) ;  
~  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:37pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/12 "2005-11-26T15:37:45Z")

</div>

Craig,

This, as far as I understand it, is done voor both Admin and Store  
controller  
in the application.rb. See below:

class ApplicationController \< ActionController::Base  
model :cart  
model :line\_item  
end

On Saturday 26 November 2005 15:25, Craig W. tried to type  
something  
like:

> Yours,  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> –  
> “Who cares if it doesn’t do anything? It was made with our new  
> Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 3:29pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/13 "2005-11-26T15:29:42Z")

</div>

Derek,

This is the display\_cart method.

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

It feels like an initial setting is missing. In  
app/controllers/application.rb  
The following is set.

class ApplicationController \< ActionController::Base  
model :cart  
model :line\_item  
end

I’ll try your debugging suggestions and get back to you. BTW, what does  
the  
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type  
something  
like:

> ```
> end
> 
> ```
> 
> and see what’s displayed in Webrick’s log.
> 
> > ```
> > redirect_to(:action => 'display_cart')
> > 
> > ```
> > 
> > 8:
> > 
> > > > You have a nil object when you didn’t expect it!  
> > > > –  
> > > > –  
> > > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > > > –

~  
~  
:wq!

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 26, 2005, 5:02pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/14 "2005-11-26T17:02:24Z")

</div>

On 26 Nov 2005, at 14:34, Gerard wrote:

> This, as far as I understand it, is done voor both Admin and Store  
> controller  
> in the application.rb. See below:
> 
> class ApplicationController \< ActionController::Base  
> model :cart  
> model :line\_item  
> end

This makes sure that the models are available to your application  
when it tries to restore the session. You still need to set the  
variables to an initial value in Cart as shown in page 84, listing  
“File 76.”

After this is done you may need to clear out your ruby sessions.

## Yours, Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
[Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 28, 2005, 10:57am UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/15 "2005-11-28T10:57:41Z")

</div>

Craig,

Thanx again for your patience. The variables are being set. When I put  
the  
following code (page 88) inplace, it seems to work ok this time:

def add\_product(product)  
item = @items.find {|i| i.product\_id == product.id}  
if item  
item.quantity += 1  
else  
items = LineItem.for\_product(product)  
@items \<\< item  
end  
@total\_price += product.price  
end

Can the ruby session info mess with this, because earlier this didn’t  
work and  
now it does. Note: I now also restarted webrick, removed the ruby  
session,  
and remove the cookie in the browser.

I now end up with the display\_cart method that doesn’t work.

NoMethodError in Store#display\_cart

Showing app/views/store/display\_cart.rhtml where line #6 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.product

Two more Q’s:

- How does it get to the object nil.product?
- Is there a way to enable extensive logging, because logfiles are very  
usefull.

Thanx very much again for your feedback!

Regards,

Gerard.

On Saturday 26 November 2005 16:58, Craig W. tried to type  
something  
like:

> This makes sure that the models are available to your application  
> [Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mailing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@mailing](https://www.ruby-forum.com/u/mailing)\
**Post date:** [November 28, 2005, 11:34am UTC](https://www.ruby-forum.com/t/nomethoderror-in-store-add-to-cart/48855/16 "2005-11-28T11:34:14Z")

</div>

Craig,

Solved it! It was a friggin’ typo.

At “-\>” the first word shouldn’t be plural. I humbly thank you for  
spending  
your precious time on my human error … @-)

Kind regards,

Gerard.

def add\_product(product)  
item = @items.find {|i| i.product\_id == product.id}  
if item  
item.quantity += 1  
else  
 → items = LineItem.for\_product(product)  
@items \<\< item  
end  
@total\_price += product.price  
end

On Saturday 26 November 2005 16:58, Craig W. tried to type  
something  
like:

> This makes sure that the models are available to your application  
> [Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!
