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: <h1> My Agile Cart</h1> 2: <ul> 3: <% for item in @cart.items %> 4: <li><%= item.title %></li> 5: <% end %> 6: </ul> --------------------------------------------------------------------- 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 Son "A beautiful woman is like code sometimes, it looks perfect, but just doesn''t work..." -me -- Posted via http://www.ruby-forum.com/.
Maybe you''re missing the has_many :items in the Cart model? -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
Hi Dominic, Dominic Son 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.eachI don''t have the 2nd Edition and so don''t want to assume.... Which view is this? Best regards, Bill
Grant Ammons
2006-Jun-16 14:07 UTC
[Rails] Weird issues when controller is in subfolder of controllers/
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 Son 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: <h1> My Agile Cart</h1> > 2: <ul> > 3: <% for item in @cart.items %> > 4: <li><%= item.title %></li> > 5: <% end %> > 6: </ul>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. -- Posted via http://www.ruby-forum.com/.
Alan Francis wrote:> Dominic Son 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: <h1> My Agile Cart</h1> >> 2: <ul> >> 3: <% for item in @cart.items %> >> 4: <li><%= item.title %></li> >> 5: <% end %> >> 6: </ul> > > 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). -- Posted via http://www.ruby-forum.com/.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Fri, Jun 16, 2006 at 11:40:55AM +0200, Dominic Son wrote:> private > def find_cart > session[:cart] ||= Cart.new > endShould be: @cart = (session[:cart] ||= Cart.new) - -- Peter Harkins - 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 Son Peter Harkins wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Fri, Jun 16, 2006 at 11:40:55AM +0200, Dominic Son wrote: >> private >> def find_cart >> session[:cart] ||= Cart.new >> end > > Should be: > @cart = (session[:cart] ||= Cart.new) > > - -- > Peter Harkins - 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------- Posted via http://www.ruby-forum.com/.