NameError in StoreController#add_to_cart

Ok this is the error which I am getting it from ruby on rails

#{RAILS_ROOT}/app/controllers/store_controller.rb:8:in `add_to_cart’

and this is the code which i have write it

Code:

class StoreController < ApplicationController
def index
@products = Product.salable_items
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

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

@cart = find_cart is the line 8

can some one explain me here what to do?

Ok I have fix it in the end, this is the code which is now working

class StoreController < ApplicationController

def index
  @products = Product.salable_items
end

def add_to_cart
  product = Product.find(params[:id])
  @cart = find_cart
  @cart.add_product(product)
  redirect_to(:action => 'display_cart')
end
def display_cart
  @cart = find_cart
  @items = @cart.items
end

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

the add_to_cart( ) method is called as a action and it has to be add it
above the private method.

thanks for your support, even that in the end i fix it but still i am
happy to be a member of this community.
best regards dan,

Bogdan Grosu wrote:

Ok this is the error which I am getting it from ruby on rails

#{RAILS_ROOT}/app/controllers/store_controller.rb:8:in `add_to_cart’

and this is the code which i have write it

Code:

class StoreController < ApplicationController
def index
@products = Product.salable_items
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

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

@cart = find_cart is the line 8

can some one explain me here what to do?