# NoMethodError in PublicController

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-publiccontroller/164700>\
**Category:** Rails\
**Created:** [April 4, 2009, 8:44am UTC](https://www.ruby-forum.com/t/nomethoderror-in-publiccontroller/164700 "2009-04-04T08:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2009, 8:44am UTC](https://www.ruby-forum.com/t/nomethoderror-in-publiccontroller/164700/1 "2009-04-04T08:44:50Z")

</div>

Hello,  
I’m following a tutorial with a public controller, line\_item model,  
and cart model. When I try to add an item to the cart I am getting an  
error

NoMethodError in PublicController#add\_to\_cart

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.\<\<

Here is my code

[http://pastie.org/436638](http://pastie.org/436638)

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-publiccontroller/164700/2 "2009-04-04T12:43:26Z")

</div>

On Apr 4, 7:43 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

On the face of it this doesn’t make sense - the only thing you call \<\<  
on is @items and that is initialized to [] in the Cart’s initialize  
method. My guess would be that this was missing in your first attempt  
and although you have now fixed this, the session still contains a bad  
object with nil @items. Restarting the browser would fix it if this  
was the problem (as well as outlining one of the pitfalls with storing  
complex objects in the session).

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-publiccontroller/164700/3 "2009-04-04T19:27:56Z")

</div>

Thanks for the reply. Restarting the browser did help, however  
another one arose which is the sessions. I’m following the lynda ruby  
on rails essentials which is an older version of rails. No where in  
the tutorial did they go into detail about sessions, so I assume it’s  
more complicated with my newer version. Any suggestions as to what I  
should do next? Scrap the lynda tutorial and do another? I also  
haven’t upgrade to the newest rails version.

Brad

On Apr 4, 3:42 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
