无法在 ruby​​/ruby-on-rails 中将字符串转换为整数

无法在 ruby​​/ruby-on-rails 中将字符串转换为整数

问题描述:

我想验证用户是否存在,如果存在,进行密码匹配.

I want to verify if a user exists, and if so, do a password match.

我的控制器如下所示:

def attempt_login
    authorized_user = User.authenticate(params[:username], params[:password])
    if authorized_user
        flash[:notice] = "Successfully logged in."
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end

模型看起来像:

def self.authenticate(username="", password="")
    user = User.find_by_username(username)
    if user && user.password_match?(password)
        return user
    else
        return false
    end
end

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

在执行此操作的过程中,我收到 TypeError (Can't convert String into Integer).我怀疑当我想为 authorized_user 设置一个值时会发生错误,但不知道如何解决这个问题.

In the process of doing this, I receive the TypeError (Can't convert String into Integer). I suspect the error occurs when I want to set a value for authorized_user, but do not know how to approach this issue.

我的服务器日志是空的.但我可以发布框架跟踪:

My server log is empty. But I can post the Framework Trace:

activesupport (3.0.8) lib/active_support/descendants_tracker.rb:23:in `delete'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:23:in `block in clear'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:21:in `each'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:21:in `clear'
railties (3.0.8) lib/rails/application/bootstrap.rb:59:in `block (2 levels) in <module:Bootstrap>'
activesupport (3.0.8) lib/active_support/callbacks.rb:420:in `_run_call_callbacks'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.8) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.8) lib/rails/application.rb:168:in `call'
railties (3.0.8) lib/rails/application.rb:77:in `method_missing'
railties (3.0.8) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
E:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

如果有帮助,这里是 development.log:

And here is the development.log if it could help:

Started POST "/access/attempt_login" for 127.0.0.1 at 2011-06-27 20:19:19 +0200
DEPRECATION WARNING: config.action_view.debug_rjs will be removed in 3.1, from 3.1 onwards you will need to install prototype-rails to continue to use RJS templates . (called from <top (required)> at G:/Projects/basicsocial/app/controllers/application_controller.rb:1)
Processing by AccessController#attempt_login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jvGVYJEypK9sWoaaa5c2OwzBKmCMX7h7Wp28vBH9wfw=", "username"=>"test88", "password"=>"[FILTERED]", "commit"=>"Log In"}
<-[1m<-[36mSQL (7.0ms)<-[0m  <-[1mdescribe `users_pages`<-[0m
<-[1m<-[35mSQL (2.0ms)<-[0m  SHOW TABLES
<-[1m<-[36mUser Load (0.0ms)<-[0m  <-[1mSELECT `users`.* FROM `users` WHERE `users`.`username` = 'test88' LIMIT 1<-[0m
Redirected to http://localhost:3000/admin
Completed 302 Found in 545ms

TypeError (can't convert String into Integer):
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (5.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)

salt 变量是我用来更好地保护用户页面的变量.这是存储在数据库中的东西.

The salt variable is what I use to better secure my user's page. This is something that is stored in the database.

显然代码没有问题.这可能与我的浏览器缓存或类似的东西有关,因为它在第二天开始工作.很抱歉给您带来不便.

Apparently there was nothing wrong with the code. It was probably related to my browsers cache or something of that nature, as it started working the next day. Sorry for the inconvenience.