Need help...NameError in InteractController#add_to_cart

Hi,
I am developing a web app and am following the text-Agile web development…

Each time I click my “add to cart” button" i always get the page below:I have double checked to see if i have errors in my code,there was none,please what do i need to do to proceed?

NameError in InteractController#add_to_cart

undefined local variable or method `find_cart' for #<InteractController:0x1042d0780>

RAILS_ROOT: /Users/oluwayomioluwadara/Documents/CED/coursework/heritage/heritage

Application Trace | Framework Trace | Full Trace

/Users/oluwayomioluwadara/Documents/CED/coursework/heritage/heritage/app/controllers/interact_controller.rb:8:in `add_to_cart'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:1331:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:1331:in `perform_action_without_filters' 

/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/filters.rb:617:in `call_filters’Regards and Respects,

Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

Though I have only been playing with Rails for about a month, I think I can
answer this one.

You need to define add_a_cart in the store_controller.rb.

Good luck.

*susan*

This is what I have got.Will appreciate it if you can go through this and amend.Note the method highlighted.

class InteractController < ApplicationController

def index

@products = Product.find_products_for_sale

end

def add_to_cart

product = Product.find(params[:id])

**@cart = find_cart **

@cart.add_product(product)

**rescue ActiveRecord::RecordNotFound **

logger.error(“Attempt to access invalid product #{params[:id]}”)

flash[:notice] = "Invalid product"

** redirect_to :action =>
‘index’**

end

def empty_cart

session[:cart] = nil

flash[:notice] = " Now,your cart is currently empty"

redirect_to :action => ‘index’

end

private

def redirect_to_index(msg)

flash[:notice] = msg

redirect_to :action => ‘index’

end

end

Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

Oluwayomi Oluwadara wrote:

Hi,
I am developing a web app and am following the text-Agile
web development…

Each time I click my “add to cart” button" i always get
the page below:I have double checked to see if i have errors in my
code,there was none,please what do i need to do to proceed?

NameError in InteractController#add_to_cart

undefined local variable or method `find_cart' for #<InteractController:0x1042d0780>

You need a method 'find_cart'

I think i have the method, could you have a quick glance at my code:Will apreciate any correction or omission.

class InteractController < ApplicationController

def index

@products = Product.find_products_for_sale

end

def add_to_cart

product = Product.find(params[:id])

**@cart = find_cart **

@cart.add_product(product)

**rescue ActiveRecord::RecordNotFound **

logger.error(“Attempt to access invalid product #{params[:id]}”)

flash[:notice] = "Invalid product"

** redirect_to :action =>
‘index’**

end

def empty_cart

session[:cart] = nil

flash[:notice] = " Now,your cart is currently empty"

redirect_to :action => ‘index’

end

private

def redirect_to_index(msg)

flash[:notice] = msg

redirect_to :action => ‘index’

end

end

Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

I think i have the method, could you have a quick glance at my code:Will apreciate any correction or omission.

Where? I cannot see the method find_cart in the code you have posted.

By the way top posting is disapproved of here, it is easier to follow
a thread if you insert your reply in the appropriate place in the
previous post.

Colin

Hello,
Thanks for your time.my bad! i never figured that out.Can you help out with the find_cart method.Will greatly appreciate
Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

Hello,
Thanks for your time.my bad! i never figured that out.Can you help out with the find_cart method.Will greatly appreciate

Did you see the comment on top posting in my previous post?

It is always worth looking carefully at error messages as sometimes
they contain useful information. Yours said "undefined local variable
or method `find_cart'" which could have given you a clue to the
problem.

I am afraid I cannot help out with it as I have no idea what your
application is supposed to do. Presumably you can find something
about it in the tutorial you are following.

For beginners I always recommend working through the Rails Guides,
particularly the one on Getting Started. See
http://guides.rubyonrails.org/

Colin

Hi,
Check if find_cart may be some method which you must be missing, either check your helper file too.

this is what i have

class InteractController < ApplicationController
def index
@products = Product.find_products_for_sale
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
rescue ActiveRecord::RecordNotFound

logger.error(“Attempt to access invalid product #{params[:id]}”)

flash[:notice] = "Invalid product"
redirect_to :action => 'index'

end
def empty_cart
session[:cart] = nil
flash[:notice] = " Now,your cart is currently empty"
redirect_to :action => ‘index’
end

private
def redirect_to_index(msg)
flash[:notice] = msg
redirect_to :action => ‘index’
end
end

–Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to
love.

Hello,
thank you for your time.I am actually developing an e commerce website for selling books online.the find_cart method is mean to find the cart displayed at the front end-for the users buying stuffs.Your help wil be appreciated,am just 2 weeks old in RoR
Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

  • which tutorial are u following?
  • can u show ur cart-model?

Oluwayomi Oluwadara wrote:

Hello,
thank you for your time.I am actually developing an e
commerce website for selling books online.the find_cart method is mean
to find the cart displayed at the front end-for the users buying
stuffs.Your help wil be appreciated,am just 2 weeks old in RoR

    Regards and Respects,

In the sample application used in ‘Agile Web Development with Rails’
they define a Cart class (which is not derived from
ActiveRecord::Base). In their StoreController class they define a
find_cart method as:


def find_cart

  @cart = (session[:cart] ||= Cart.new)

end

Whether this has any applicability to your case is impossible to know
without knowing a lot more about your application.

i am actually following the book because am doing EXACTLY the same thing.this is my code, have a look at time .will appreciate corrections.i am sick of seeing the error message.

class InteractController < ApplicationController

def index

@products = Product.find_products_for_sale

end

def add_to_cart

product = Product.find(params[:id])

@cart = find_cart

@cart.add_product(product)

rescue ActiveRecord::RecordNotFound

logger.error(“Attempt to access invalid product #{params[:id]}”)

flash[:notice] = “Invalid product”

redirect_to :action => ‘index’

end

def empty_cart

session[:cart] = nil

flash[:notice] = " Now,your cart is currently empty"

redirect_to :action => ‘index’

end

private

def redirect_to_index(msg)

flash[:notice] = msg

redirect_to :action => ‘index’

end

end Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

I am following the text" agile web development with rails"
here is my code,

class InteractController < ApplicationController

def index

@products = Product.find_products_for_sale

end

def add_to_cart

product = Product.find(params[:id])

@cart = find_cart

@cart.add_product(product)

rescue ActiveRecord::RecordNotFound

logger.error(“Attempt to access invalid product #{params[:id]}”)

flash[:notice] = “Invalid product”

redirect_to :action =>
‘index’

end

def empty_cart

session[:cart] = nil

flash[:notice] = " Now,your cart is currently empty"

redirect_to :action => ‘index’

end

private

def redirect_to_index(msg)

flash[:notice] = msg

redirect_to :action => ‘index’

end

end

Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

There is no point repeatedly posting the same code. As has been
pointed out several times the problem is that you are missing the
find_cart method. In Agile Development With Rails this is in the
StoreController, as has also been pointed out to you previously. I
suggest you go back to the book and have another look. I believe you
can even download the application source if you wish.

Colin

thanks a bunch
Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.

Hello,
when i added the find-cart method(private access) that the controller is loking for, am now getting this syntax error message.Ithis is my method:

private

def find_cart

  session[:cart] ||= Cart.new

end

SyntaxError in InteractController#add_to_cart
/Users/oluwayomioluwadara/Documents/CED/coursework/heritage/heritage/app/models/cart.rb:19: syntax error, unexpected $end, expecting kEND

Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to
eloquence.
Kindness in action leads to love.

The error is in your model/cart.rb around line 19 as the error message
indicates. Read the error messages closely and you will find a lot of
information. The error indicates that you have a syntax error in
model/cart.rb. Maybe a block opened without an end or quotes not
matching or parentheses not matching etc.

Hello,
thank you.Ut worked wonders!
Regards and Respects,
Kindness in thought leads to wisdom.
Kindness in speech leads to eloquence.
Kindness in action leads to love.