Wrong number of arguments (1 for 0)


#1

Hello,
I am new to ruby on rails. When I tried to create a session I am
getting an error, ‘wrong number of arguments (1 for 0)’, in my
controller.

 ArgumentError in CustomerController#add_to_cart

wrong number of arguments (1 for 0)

RAILS_ROOT: /project/bob_shopping
Application Trace | Framework Trace | Full Trace

app/controllers/customer_controller.rb:36:in `session'
app/controllers/customer_controller.rb:36:in `add_to_cart'

My controller is …

def add_to_cart
if request.post?
product_id = params[:product_id]
quantity = params[:quantity]
account = get_account()
error)—> session[:cart] = SessionCart.new(account) if
session(:cart).nil?
session[:cart].add_product( product_id)
if quantity >= 2 and quantity <= 50
for i in 1…quantity-1
session[:cart].add_product(product_id)
end
end
flash[:notice] = ‘Your order has been placed.’
redirect_to :action=>‘index’ and return true
end
end


Waiting for your valuable reply..

#2

change
session(:cart).nil?

to
session[:cart].nil?

session is not a method call

On Feb 18, 12:30 pm, Sony S. removed_email_address@domain.invalid


#3

On Feb 18, 5:44 pm, jemminger removed_email_address@domain.invalid wrote:

change
session(:cart).nil?

to
session[:cart].nil?

session is not a method call

If we’re picking nits it is :slight_smile: - you just don’t pass any arguments to
it.

Fred


#4

It would be helpful to see the CustomerController code for the new
method, but…

I have to assume that new should be called as:

session[:cart] = SessionCart.new() if session(:cart).nil?

On Feb 18, 7:30 am, Sony S. removed_email_address@domain.invalid