NameError in StoreController#add_to_cart uninitialized constant StoreController::Cart This is the controller: class StoreController < ApplicationController def index @products = Product.find_products_for_sale 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 Amd this is the model class Cart attr_reader :items def initialize @items = [] end def add_product(product) @items << product end end Thanks for the 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 Jul 10, 2008, at 5:51 PM, Justin To wrote:> > NameError in StoreController#add_to_cart > > uninitialized constant StoreController::Cart > > This is the controller: > > class StoreController < ApplicationController > def index > @products = Product.find_products_for_sale > 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 > > Amd this is the model > > class Cartclass Cart < ActiveRecord::Base Did you just miss < ActiveRecord::Base when writing the email? Best. Mike> > attr_reader :items > > def initialize > @items = [] > end > > def add_product(product) > @items << product > end > end > > Thanks for the 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 -~----------~----~----~----~------~----~------~--~---
=P... Although, I am getting this error now: undefined method `add_product'' for []:Array -- 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 -~----------~----~----~----~------~----~------~--~---
No 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 -~----------~----~----~----~------~----~------~--~---
Can you add items to the cart in the console? This is wild speculation, I''ve never made an app with a cart, but seems like you might need a has_many :items, :class_name => ''products'' in the Cart model? On Jul 10, 6:18 pm, Justin To <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> No help? =( > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
maybe your file name is Cart.rb instead of cart.rb Marnen Laibow-Koser-2 wrote:> > > NameError in StoreController#add_to_cart > > uninitialized constant StoreController::Cart > > This is the controller: > > class StoreController < ApplicationController > def index > @products = Product.find_products_for_sale > 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 > > Amd this is the model > > class Cart > attr_reader :items > > def initialize > @items = [] > end > > def add_product(product) > @items << product > end > end > > Thanks for the help > -- > Posted via http://www.ruby-forum.com/. > > > > >-- View this message in context: http://www.nabble.com/Uninitialized-constant-%28controller-model%29-tp18392649p26123953.html Sent from the RubyOnRails Users mailing list archive at Nabble.com.