I get the following: NoMethodError in Shoppingcart#my_shopping_cart Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #33 raised: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.shopping_cart_items The line in question is <% for shopping_cart_item in @shopping_cart.shopping_cart_items %> Any suggestions? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2008-Nov-27 04:03 UTC
Re: You have a nil object when you didn''t expect it!
> I get the following: > > NoMethodError in Shoppingcart#my_shopping_cart > > Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #33 > raised: > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.shopping_cart_items > > The line in question is > > <% for shopping_cart_item in @shopping_cart.shopping_cart_items %> > > Any suggestions?@shopping_cart is nil. You''ll need to look at the my_shopping_cart action of the Shoppingcart controller to figure out why that''s not being set correctly. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Philip. I''m new here, can you advise on what I need to do prior to get @shopping_cart set? I''m not sure of the exact steps involved in accessing the data in ROR. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So I made a change and now I have: NameError in Shoppingcart#my_shopping_cart Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #33 raised: undefined local variable or method `shopping_cart_item'' for Shopping_cart:Class Not sure where to go from here. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s what I now have, not sure where to go from here: NameError in Shoppingcart#my_shopping_cart Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #24 raised: undefined local variable or method `shopping_cart_item'' for Shopping_cart:Class Extracted source (around line #24): 21: <td valign="top" width="50%"></b>Traded items: </b><br> 22: <table> 23: <% if @my_cart 24: for shopping_cart_item in @my_cart.shopping_cart_items %> 25: <tr> 26: <t -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 27, 4:24 pm, Joe Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Here''s what I now have, not sure where to go from here: > > NameError in Shoppingcart#my_shopping_cart > > Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #24 > raised: > > undefined local variable or method `shopping_cart_item'' for > Shopping_cart:Class > Extracted source (around line #24): > > 21: <td valign="top" width="50%"></b>Traded items: </b><br> > 22: <table> > 23: <% if @my_cart > 24: for shopping_cart_item in @my_cart.shopping_cart_items %> > 25: <tr> > 26: <t > --What does your Shopping_cart class look like? How is shopping_cart_items defined? -- Daniel Bush --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
class Shopping_cart < ActiveRecord::Base belongs_to :owner, :class_name => "user", :foreign_key => "owner_id" has_many :shopping_cart_items, :class_name => "shopping_cart_item", :finder_sql => "select ti.* from shopping_cart_items ti, shopping_carts t where ti.owner_id = t.owner_id" end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 27, 4:47 pm, Joe Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> class Shopping_cart < ActiveRecord::Base > belongs_to :owner, > :class_name => "user", > :foreign_key => "owner_id" > > has_many :shopping_cart_items, > :class_name => "shopping_cart_item", > :finder_sql => "select ti.* from shopping_cart_items ti, > shopping_carts t > where ti.owner_id = t.owner_id" > end > --Your shopping_cart_item (singular) is probably a rails model and it needs to start with a capital letter as it should be a ruby constant. When you define a model or any class in the normal way, you do something like this class ShoppingCartItem < ActiveRecord::Base ... ShoppingCartItem is a ruby constant because it starts with capital (as opposed to local variable or method name). Also, you should probably use camel case instead of underscores for constant names - that seems to be a convention but with rails it might be a requirement (not completely sure as I''ve never done it that way). You''d then refer to this class using class_name => ''ShoppingCartItem'' . Your underlying table can stay as shopping_cart_items, of course. You could probably dispense with class_name altogether if "has_many :shopping_cart_items" refers to model ShoppingCartItem. Rails will infer that for you. -- Daniel Bush --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel, thank you so much, now I have the message NameError in Shoppingcart#my_shopping_cart Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #25 raised: uninitialized constant Shopping_cart::ShoppingCartItems Extracted source (around line #25): 23: <% if @my_cart 24: 25: for shopping_cart_item in @my_cart_items.shopping_cart_items %> 26: <tr> My shoppingcart.rb is class Shopping_cart < ActiveRecord::Base belongs_to :owner, :class_name => "user", :foreign_key => "owner_id" has_many :shopping_cart_items, :class_name => "ShoppingCartItems", :finder_sql => "select ti.* from shopping_cart_items ti, shopping_carts t where ti.owner_id = t.owner_id" end and my shoppingcartitem.rb defines class ShoppingCartItem I feel like I''m close, but I can''t figure this out. Can you figure out the missing link? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I got it working, thanks for your help -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 27, 7:11 pm, Joe Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I got it working, thanks for your help > -- > Posted viahttp://www.ruby-forum.com/.You can name your files like: shopping_cart_item.rb but define the class inside as ''ShoppingCartItem''. Similarly for shopping_cart.rb and class ShoppingCart. Glad it helped. -- Daniel Bush --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---