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

前端之家收集整理的这篇文章主要介绍了无法在ruby / ruby​​-on-rails中将String转换为整数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想验证用户是否存在,如果存在,请执行密码匹配.

我的控制器看起来像这样:

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(无法将String转换为Integer).我怀疑当我想为authorized_user设置一个值时发生错误,但不知道如何处理这个问题.

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

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'

这是develop.log,如果它可以帮助:

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变量是我用来更好地保护用户页面方法.这是存储在数据库中的东西.

解决方法

显然代码没有任何问题.它可能与我的浏览器缓存或某种性质有关,因为它在第二天就开始工作了.抱歉给你带来不便.
原文链接:https://www.f2er.com/ruby/274270.html

猜你在找的Ruby相关文章