用户身份验证尝试将字符串转换为整数

时间:2012-01-28 19:56:43

标签: ruby-on-rails-3 authentication

  

可能重复:
  Can't convert String into integer in ruby/ruby-on-rails

我正在浏览Lynda的教程,学习rails并且我遇到了问题。我已经构建了一个AccessController来使用我拥有的AdminUser模型。我使用字符串/时间盐组合并将其保存到Db,然后尝试使用登录表单进行身份验证。以下是AccessController的一些代码

  def attempt_login
authorized_user = AdminUser.authenticate(params[:username], params[:password])
#authorized_user = nil

if authorized_user
    flash[:notice] = "You are logged in now"
    redirect_to(:action => 'menu')
else
    flash[:notice] = "invalid username/password combo"
    redirect_to(:action => 'login')
end

然后我的模型中的一些我定义了authenticate方法:

    def self.authenticate(username="", password="")

    user = AdminUser.find_by_username(username)
    if user && user.password_match?(password)
        return user
    else
        return false
    end
end

    def password_match?(password="")
    hashed_password == AdminUser.hash_with_salt(password, salt)
end

所有hash_with_salt方法都会从该用户的数据库中获取salt,并确保输入的密码使用相同的盐进行加盐/加密。这是我不断得到的错误:

TypeError in AccessController#attempt_login

can't convert String into Integer

Rails.root: /Users/UNAME/Sites/simple_cms
Application Trace | Framework Trace | Full Trace

activesupport (3.1.3) lib/active_support/descendants_tracker.rb:22:in `delete'
activesupport (3.1.3) lib/active_support/descendants_tracker.rb:22:in `block in clear'
activesupport (3.1.3) lib/active_support/descendants_tracker.rb:20:in `each'
activesupport (3.1.3) lib/active_support/descendants_tracker.rb:20:in `clear'
railties (3.1.3) lib/rails/application/bootstrap.rb:56:in `block (2 levels) in <module:Bootstrap>'
activesupport (3.1.3) lib/active_support/callbacks.rb:395:in `_run_cleanup_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.3) lib/action_dispatch/middleware/reloader.rb:72:in `rescue in call'
actionpack (3.1.3) lib/action_dispatch/middleware/reloader.rb:67:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.3) lib/rails/engine.rb:456:in `call'
rack (1.3.6) lib/rack/content_length.rb:14:in `call'
railties (3.1.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/Users/UNAME/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/UNAME/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/UNAME/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

非常感谢任何帮助。我确定我做的与教程完全相同,但由于Ruby / Rails版本不同,可能会出现错误?搜索了几个小时,大多数结果讨论了inject()方法,这没有用。再次感谢!

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,花了好几个小时试图修复它。事实证明,我已经离开了一个方法,教程告诉我事先删除了一些视频:

 def self.hash(password="")
   Digest::SHA1.hexdigest(password)
 end

在本教程中,这已被方法

取代
 def self.make_salt(username="")
   Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
 end

 def self.hash_with_salt(password="", salt="")
   Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
 end

一旦我评论了这个方法,错误就消失了。我仍然不明白为什么这个错误首先出现了。我添加了一个问题和更多细节here

答案 1 :(得分:0)

以下是导致该错误的一些常见方法:

my_method :name => "dan", :age => 23

def my_method(*args) 
  puts args[:name] # ERROR. thinks that args looks like {:name => "dan3, :age => 23}
  puts args[0][:name] # SUCCESS. args is actually an array because of the *splat: 
                      # args = [{:name => "dan", :age => 23}]
end

或者

params = {}
params[:greeting] = "howdy"
params[:greeting][:bye] # ERROR. You're basically calling "howdy"[:bye].
                        # Note that "howdy"[0] returns "h".

这不是一个答案,但它很难作为评论阅读。也许它可以帮助你看看要找什么。

例如,您传递字符串的某些代码可能需要散列。