Forum: Ruby on Rails no method error - you have a nil object....

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
De60a566d8e2a8dec790d1f6741ad424?d=identicon&s=25 Jay Johnston (shambolic)
on 2006-02-12 19:43
hi,

i've been cracking my head on this one for a while and would be humbly
grateful for any pointers

i'm working through Agile Web Devopment w Rails and, in the depot
example have created the cart class for session handling, but am getting
the following error, which suggests the @items variable is not being
populated with any data from LineItem

NoMethodError (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.<<):
    /app/models/cart.rb:12:in `add_product'
    /app/controllers/store_controller.rb:10:in `add_to_cart'

i've triple-checked the code, and found the error in the book through
the errata, but really am stumped by this error.
any suggestions on good troubleshooting techniques .  i have tried rake
- which says it can't connect to mysql through the socket file - even
though the rest of the db access seems to work fine.   i suspect this is
part of the problem, seeing as the variable is empty, but don't know how
to pin it down .

i've also tried  irb store_controller, which seems fine, except for:

NameError: uninitialized constant ApplicationController
        from app/controllers/store_controller.rb:1


code (with my some of my own comments as follows). once again, thanks in
advance:

class Cart

  attr_reader :items
  attr_reader :total_price

  def initialise
    @items = []
    @total_price = 0.0
  end

  def add_product(product)
    @items << LineItem.for_product(product)  #uses helper class method
"for_product" in model: line_item.rb, which creates a line_item based on
a given product
    @total_price += product.price
  end
end
=====
class StoreController < ApplicationController

  def index  #  calls salable_items function in the controller to create
a list of items that are available for sale depending on date
    @products = Product.salable_items
  end

  def add_to_cart #  method to add product with :id to the cart with
redirect to display
    product = Product.find(params[:id])
    @cart = find_cart  # gets existing cart object or creates new one
    @cart.add_product(product)  # add product method sits in the Cart
class (in models)
    redirect_to(:action => 'display_cart')
  end

  def display_cart
    @cart = find_cart
    @items = @cart.items
  end

  private   # everything below here is private.  any public actions need
to be above this part of the class
  def find_cart  # creates a new cart session object if one doesn't
exist pg 81
    @cart = session[:cart] ||= Cart.new
  end

end
This topic is locked and can not be replied to.