OffRailed, and stuck in the book : (

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

Maybe you’re missing the has_many :items in the Cart model?

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.

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

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

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.

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).

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

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

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

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