08 Mar 2009, 16:15
Mittelalter_002_pragsmall

Philipp Ringli (6 posts)

I got until page 111 and am stuck with a 500 error:

Status: 500 Internal Server Error undefined class/module Cart

I’ve got the latest version of the book. This is my first contact with rails or ruby.

Cheers, Phil

09 Mar 2009, 13:32
Mittelalter_002_pragsmall

Philipp Ringli (6 posts)

The problem is with the commented lines. Using the verbose version works. Could someone explain?

from store_controller.rb

def find_cart 
    unless session[:cart] # if there's no cart in the session 
      session[:cart] = Cart.new # add a new one 
  end 
    session[:cart] # return existing or new cart 
  end 

  # def find_cart 
  #   session[:cart] ||= Cart.new 
  # end
09 Mar 2009, 14:33
Samr_small_pragsmall

Sam Ruby (584 posts)

Please verify that you have a file by the name of “app/models/cart.rb”:http://media.pragprog.com/titles/rails3/code/depot_f/app/models/cart.rb, and that that file starts with the line “class Cart”.

It might also be helpful to try the following to verify that the class is in place:

$ ruby script/console 
Loading development environment (Rails 2.3.1)
>> Cart
=> Cart

If all else fails, restart your server.

10 Mar 2009, 19:01
Generic-user-small

webb chang (1 post)

Novice’s suggestion: 1. comment out the first find_cart method. 2. there’s not space between |and |

I got trapped into such minor details before.

12 Mar 2009, 06:42
N28108761_40674795_5309_pragsmall

Slava Sakhnenko (1 post)

I stalled at exactly the same point, whenever I try to add a Cart to a session it gives me:

Parameters: {"authenticity_token"=>"2Lv2I2/fD4xP2MnGoJIblE8LjjljfHmh39Eu9ZxQEi0=", "id"=>"4"}
  Product Columns (4.0ms)   SHOW FIELDS FROM `products`
  Product Load (1.0ms)   SELECT * FROM `products` WHERE (`products`.`id` = 4) 
Rendering template within layouts/store
Rendering store/add_to_cart
Completed in 91ms (View: 57, DB: 6) | 200 OK [http://www.horizonglow.com/store/add_to_cart/4]
/!\ FAILSAFE /!\  Thu Mar 12 02:13:27 -0400 2009
  Status: 500 Internal Server Error
  undefined class/module Cart

However, this only appears when I try to add the Cart into a session(:cart)

For instance: This would run just fine (without doing anything)

def find_cart
    unless session[:cart]
        cart = Cart.new
    end
    session[:cart]
end

But while running the code below, it would throw that ‘undefined class’ error.

def find_cart
    unless session[:cart]
        session[:cart] = Cart.new
    end
    session[:cart]
end

It’s as if there’s some kind of barrier preventing the session from saving a Cart. Does anyone know anything that might have caused this?

12 Mar 2009, 15:38
Generic-user-small

David Weddle (1 post)

Looks like this example, as written, won’t run on Rails 2.3. I dropped back to 2.2.2 and the problem went away. This appears to be a “related topic”:http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1864-cant-store-non-activerecord-models-in-session

12 Mar 2009, 18:35
Mittelalter_002_pragsmall

Philipp Ringli (6 posts)

Sam, thanks for your help. Somehow the problem just went away. I had the class in place properly. I cannot reconstruct why I got the error anymore. :(

bq.Novice’s suggestion: bq.2. there’s not space between |and | I had no space between the pipes. It’s the or operator.

  You must be logged in to comment