Invalid number of arguments [ex: (3 for 2)] in methods [but only pass 2]

Pretty strange problem:

session_controller:

user = User.authenticate(params[:name], params[:password])

user.rb:

    def authenticate(name, password)
      if user = find_by_name(name)
        if user.hashed_password == encrypt_password(password, user.salt)
        user
        end
      end
    end

I get

app/models/user.rb:10:in `authenticate'
app/controllers/sessions_controller.rb:27:in `create'

Request

Parameters:

{"utf8"=>"รขล“"",
 "authenticity_token"=>"wOQEEKnH68MKewyM2Mpxc9aRoOg8ByXuYJKrMCI17SE=",
 "name"=>"1@1.com",
 "password"=>"[FILTERED]",
 "commit"=>"Connexion"}

it happened in all methods

my package list

 Gems included by the bundle:
   * abstract (1.0.0)
   * actionmailer (3.1.0.beta)
   * actionpack (3.1.0.beta)
   * activemodel (3.1.0.beta)
   * activerecord (3.1.0.beta)
   * activeresource (3.1.0.beta)
   * activesupport (3.1.0.beta)
   * arel (2.0.8)
   * bcrypt-ruby (2.1.4)
   * builder (3.0.0)
   * bundler (1.1.pre.1)
   * capistrano (2.5.19)
   * erubis (2.6.6)
   * highline (1.6.1)
   * i18n (0.5.0)
   * jquery-rails (0.2.7)
   * mail (2.2.15)
   * mime-types (1.16)
   * net-scp (1.0.4)
   * net-sftp (2.0.5)
   * net-ssh (2.1.0)
   * net-ssh-gateway (1.0.1)
   * newrelic_rpm (2.13.4)
   * polyglot (0.3.1)
   * rack (1.2.1 e3ffeac)
   * rack-cache (1.0)
   * rack-mount (0.6.13)
   * rack-test (0.5.7)
   * rails (3.1.0.beta 289cc15)
   * railties (3.1.0.beta)
   * rake (0.8.7)
   * sqlite3 (1.3.3)
   * sqlite3-ruby (1.3.3)
   * thor (0.14.6)
   * treetop (1.4.9)
   * tzinfo (0.3.24)

This happened after use.

gem 'rails', :git => 'git://github.com/rails/rails.git'

And it was necessary to add

gem 'rack', :git => 'git://github.com/rack/rack.git'  

Can anyone get the same problem on such a simple weird problem?

Thank you for your help :)

+3
source share
1 answer

It looks like you are trying to call an instance method as a class method. This way, your code will be suitable for calling @user.authenticate(x,y)for an existing user, but for calling authenticatein your class you need to define it as shown below:

def self.authenticate(name, password)
  if user = find_by_name(name)
    if user.hashed_password == encrypt_password(password, user.salt)
    user
    end
  end
end
+3
source

Source: https://habr.com/ru/post/1793278/


All Articles