# OffRailed, and stuck in the book : (

**URL:** <https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581>\
**Category:** Rails\
**Created:** [June 16, 2006, 11:40am UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581 "2006-06-16T11:40:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 11:40am UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/1 "2006-06-16T11:40:55Z")

</div>

Help, i’ve been trapped on the tracks here for a day now in the Agile w/  
Rails book (latest edition) I can’t get page 115 to display.

It says:  
You have a nil object when you didn’t expect it!

You might have expected an instance of Array.  
The error occured while evaluating nil.each

Extracted source (around line #3):

1:

# My Agile Cart
  
2: 
  
3: \<% for item in @cart.items %\>  
4: - \<%= item.title %\>
  
5: \<% end %\>  
6: 

* * *

I’ve checked my store\_controller.rb and it is just copied and pasted:

* * *

class StoreController \< ApplicationController

def index

@products = Product.find\_products\_for\_sale  
@page\_title = “We Welcome you”  
end

def add\_to\_cart  
@cart = find\_cart  
@product = Product.find(params[:id])  
@cart.add\_product(@product)  
end

private  
def find\_cart  
session[:cart] ||= Cart.new  
end

end

* * *

I’ve looked at my mySQL, and the sessions are in there.

Any insights?

Thanks a lot.

Dominic S.

“A beautiful woman is like code sometimes,  
it looks perfect, but just doesn’t work…”  
-me

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/2 "2006-06-16T13:53:27Z")

</div>

Maybe you’re missing the has\_many :items in the Cart model?

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/3 "2006-06-16T14:17:25Z")

</div>

It looks like you’re missing something in the cart.rb class file, but  
it’s not has\_many since Cart isn’t an ActiveRecord model in that  
example.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/4 "2006-06-16T15:42:45Z")

</div>

Hi Dominic,

Dominic S. wrote:

> It says:  
> You have a nil object when you didn’t expect it!
> 
> You might have expected an instance of Array.  
> The error occured while evaluating nil.each

I don’t have the 2nd Edition and so don’t want to assume… Which view  
is  
this?

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/5 "2006-06-16T16:08:47Z")

</div>

Hey All,

So say I have a controller that is in a subfolder of controllers, say  
app/controllers/community/forums\_controller.rb.

Now say in that controller, there is a redirect\_to :controller =\> “auth”

By default, it looks for app/controllers/community/auth\_controller.  
That doesn’t seem like the correct behavior. My auth\_controller  
exists in app/controllers.

How do I access the auth\_controller without using routes, and without  
using redirect\_to “/auth/index”

Thanks!  
-Grant

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/6 "2006-06-16T16:15:16Z")

</div>

Dominic S. wrote:

> Help, i’ve been trapped on the tracks here for a day now in the Agile w/  
> Rails book (latest edition) I can’t get page 115 to display.
> 
> It says:  
> You have a nil object when you didn’t expect it!
> 
> You might have expected an instance of Array.  
> The error occured while evaluating nil.each
> 
> Extracted source (around line #3):
> 
> 1:
> 
> # My Agile Cart
>   
> 2: 
>   
> 3: \<% for item in @cart.items %\>  
> 4: - \<%= item.title %\>
>   
> 5: \<% end %\>  
> 6:

Just a little explanation to possibly help you on your way. The message  
is telling you that items is nil.

Is it possible there are no items in your cart ?

A.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/7 "2006-06-16T16:34:54Z")

</div>

Alan F. wrote:

> Dominic S. wrote:
> 
> > Help, i’ve been trapped on the tracks here for a day now in the Agile w/  
> > Rails book (latest edition) I can’t get page 115 to display.
> > 
> > It says:  
> > You have a nil object when you didn’t expect it!
> > 
> > You might have expected an instance of Array.  
> > The error occured while evaluating nil.each
> > 
> > Extracted source (around line #3):
> > 
> > 1:
> > 
> > # My Agile Cart
> >   
> > 2: 
> >   
> > 3: \<% for item in @cart.items %\>  
> > 4: - \<%= item.title %\>
> >   
> > 5: \<% end %\>  
> > 6:
> 
> Just a little explanation to possibly help you on your way. The message  
> is telling you that items is nil.
> 
> Is it possible there are no items in your cart ?
> 
> A.

Just wondering, page 115 of the PDF file or page 115 as reported by the  
page numbers on the pages? (It makes about a 15 page difference and the  
code is different in the two places.)

As Alan jsut said, it looks like your cart’s items array isn’t getting  
setup properly, make sure your Cart is defined with the initialize  
method as shown on page 113 PDF, (101 page number).

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/8 "2006-06-16T18:39:24Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Fri, Jun 16, 2006 at 11:40:55AM +0200, Dominic S. wrote:

> private  
> def find\_cart  
> session[:cart] ||= Cart.new  
> end

Should be:  
@cart = (session[:cart] ||= Cart.new)

* * *

Peter H. - [http://push.cx](http://push.cx)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)  
Comment: If you don’t know what this is, it’s OK to ignore it.

iD8DBQFEkt5sa6PWv6+ALKoRAoQoAJ4wbvu5/hT4wNQx0lIOs6oonkbu9wCdHnXA  
eFqXJ1OPRXJjcOBO4x6cMVE=  
=SjSt  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [June 16, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/offrailed-and-stuck-in-the-book/63581/9 "2006-06-16T23:07:41Z")

</div>

Peter, you are the ‘bomb’ that helped me get off and running on these  
tracks.

that was the answer. And I’m going to report this to the Agile book guys  
with your credit of course.

Dominic S.

Peter H. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> On Fri, Jun 16, 2006 at 11:40:55AM +0200, Dominic S. wrote:
> 
> > private  
> > def find\_cart  
> > session[:cart] ||= Cart.new  
> > end
> 
> Should be:  
> @cart = (session[:cart] ||= Cart.new)
> 
> * * *
> 
> Peter H. - [http://push.cx](http://push.cx)
> 
> -----BEGIN PGP SIGNATURE-----  
> Version: GnuPG v1.4.2.2 (GNU/Linux)  
> Comment: If you don’t know what this is, it’s OK to ignore it.
> 
> iD8DBQFEkt5sa6PWv6+ALKoRAoQoAJ4wbvu5/hT4wNQx0lIOs6oonkbu9wCdHnXA  
> eFqXJ1OPRXJjcOBO4x6cMVE=  
> =SjSt  
> -----END PGP SIGNATURE-----
