用户身份验证尝试字符串转换成整数 [英] User authentication trying to convert string into integer

查看:136
本文介绍了用户身份验证尝试字符串转换成整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/6497351/cant-convert-string-into-integer-in-ruby-ruby-on-rails\">Can't字符串转换成整数红宝石/红宝石上轨

我通过一个教程林达运行轨道的学习,我遇到了一个问题。我已经建立了一个AccessController的具有管理用户模式我必须工作。我使用的是串/次盐的组合,节省了到数据库,然后尝试使用登录表单进行验证。下面是一些AccessController的code

I'm running through a tutorial on Lynda for learning rails and I'm running into a problem. I've built an AccessController to work with an AdminUser model I have. I'm using a string/time salt combo and saving that to the Db, then trying to authenticate using a login form. Here is some code from 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

结束

和然后一些从我的模型中,我定义了身份验证方法:

And then some from my model where I define the authenticate method:

    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方法确实是以盐从数据库中该用户,并确保输入的密码被腌制/用相同的盐encryped。下面是我不断得到错误:

All the hash_with_salt method does is take salt from the DB for that user and makes sure the password entered is salted/encryped with the same salt. Here's the error I'm continuously getting:

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'

任何帮助将大大AP preciated。我敢肯定我在做同样的事情的教程,但也许有不同的原因的Ruby / Rails版本的错误?搜索小时这与大多数的结果讨论注入()方法,这是没有帮助的。再次感谢!

Any help would be greatly appreciated. I'm certain I'm doing the exact same thing as the tutorial, but maybe there's an error because of differing Ruby/Rails versions? Searched for hours on this with most of the results discussing the inject() method and that is not helpful. Thanks again!

推荐答案

我有这个同样的问题,花了几个小时试图修复它。原来,我已经离开的方法教程,叫我删除了一些影片事先:

I had this same problem and spent hours trying to fix it. It turned out that I had left in a method that the tutorial had told me to delete a few videos beforehand:

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

在本教程中,这也换成了方法

In the tutorial, this had been replaced by the methods

 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

在我评论这个方法出来,错误就走开了。我还是不明白,为什么这个错误摆在首位出现了。我加了一个问题,更详细的<一个href=\"http://stackoverflow.com/questions/9267484/why-does-this-unused-self-hash-method-cause-a-cant-convert-string-into-integer\">here.

Once I commented this method out, the error went away. I still don't understand why that error showed up in the first place. I added a question and more detail here.

这篇关于用户身份验证尝试字符串转换成整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆