Forum: Ruby on Rails wrong number of arguments (1 for 0)

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.
7e9a27082d5ef910d7e90949777ee795?d=identicon&s=25 Sony Sebastian (zonyzeb)
on 2009-02-18 18:30
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..
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2009-02-18 18:45
(Received via mailing list)
change
  session(:cart).nil?

to
  session[:cart].nil?

session is not a method call

On Feb 18, 12:30 pm, Sony Sebastian <rails-mailing-l...@andreas-s.net>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-18 18:52
(Received via mailing list)
On Feb 18, 5:44 pm, jemminger <jemmin...@gmail.com> wrote:
> change
>   session(:cart).nil?
>
> to
>   session[:cart].nil?
>
> session is not a method call

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

Fred
Af2ce6689213fdb78913a9662b18da6b?d=identicon&s=25 Rick (Guest)
on 2009-02-18 18:52
(Received via mailing list)
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 Sebastian <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.