Hi everyone, Very new to rails and while following the book: Beginning Ruby on Rails E-Commerce", I''m trying ti implement a cart, but the cart doesn''t appear on the html page at all. The code for the cart in app/ views/layouts/application.rhtml: <% if @cart %> <div id="shopping_cart"> <%= render :partial => "cart/cart" %> </div> <%= drop_receiving_element("shopping_cart", :url => { :controller => "cart", :action => "add" }) %> <% end %> If I take out the if @cart, I get this error: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.cart_items Extracted source (around line #7): 4: 5: <h3>Your Shopping Cart</h3> 6: <ul> 7: <% for item in @cart.cart_items %> 8: <li id="cart_item_<%= item.product.id %>"> 9: <%= render :partial => "cart/item", :object => item %> 10: </li> This code appears on app/views/cart/_cart.rhtml Please help. Anyone? TIA, Elle --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thomas Wieczorek
2007-Sep-01 13:33 UTC
Re: Error: You have a nil object when you didn''t expect it!
You have to declare the @cart variable in the controller. --~--~---------~--~----~------------~-------~--~----~ 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 Sep 2, 12:33 am, "Thomas Wieczorek" <wieczo...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> You have to declare the @cart variable in the controller.Thanks for the quick reply. Here is my cart_controller.rb: class CartController < ApplicationController before_filter :initialize_cart def add params[:id].gsub!(/product_/, "") @product = Product.find(params[:id]) if request.xhr? @item = @cart.add(params[:id]) flash.now[:cart_notice] = "Added <em>#{@item.product.title}</ em>" render :action => "add_with_ajax" elsif request.post? @item = @cart.add(params[:id]) flash[:cart_notice] = "Added <em>#{@item.product.title}</em>" redirect_to :controller => "catalog" else render end end ... and my cart.rb model: class Cart < ActiveRecord::Base has_many :cart_items has_many :products, :through => :cart_items def add(product_id) items = cart_items.find_all_by_product_id(product_id) product = Product.find(product_id) if items.size < 1 ci = cart_items.create(:product_id => product_id, :amount => 1, :price => product.price) else ci = items.first ci.update_attribute(:amount, ci.amount + 1) end ci end ... and my cart_item.rb model: class CartItem < ActiveRecord::Base belongs_to :cart belongs_to :product end What am I doing wrong? TIA, Elle --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Louis Rose
2007-Sep-03 15:20 UTC
Re: Error: You have a nil object when you didn''t expect it!
On Sep 2, 6:28 am, elle <wazne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 2, 12:33 am, "Thomas Wieczorek" <wieczo...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > > > You have to declare the @cart variable in the controller. > > Thanks for the quick reply. Here is my cart_controller.rb: > > class CartController < ApplicationController > before_filter :initialize_cart > > def add > params[:id].gsub!(/product_/, "") > @product = Product.find(params[:id]) > > if request.xhr? > @item = @cart.add(params[:id]) > flash.now[:cart_notice] = "Added <em>#...@item.product.title}</ > em>" > render :action => "add_with_ajax" > elsif request.post? > @item = @cart.add(params[:id]) > flash[:cart_notice] = "Added <em>#...@item.product.title}</em>" > redirect_to :controller => "catalog" > else > render > end > end > ... > > and my cart.rb model: > > class Cart < ActiveRecord::Base > has_many :cart_items > has_many :products, :through => :cart_items > > def add(product_id) > items = cart_items.find_all_by_product_id(product_id) > product = Product.find(product_id) > if items.size < 1 > ci = cart_items.create(:product_id => product_id, > :amount => 1, > :price => product.price) > else > ci = items.first > ci.update_attribute(:amount, ci.amount + 1) > end > ci > end > ... > > and my cart_item.rb model: > > class CartItem < ActiveRecord::Base > belongs_to :cart > belongs_to :product > end > > What am I doing wrong? > > TIA, > ElleHi Elle, What code do you have for the initialize_cart method in the CartController? Cheers, Louis. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---