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

我想validation用户是否存在,如果存在,请执行密码匹配。

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

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

显然代码没有任何问题。 它可能与我的浏览器缓存或某种性质有关,因为它在第二天就开始工作了。 抱歉给你带来不便。

我有同样的问题,并花了几个小时试图解决它。 事实certificate,我已经离开了一个方法,教程告诉我事先删除一些video:

  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 

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

只想告诉你我也有这个问题。 问题很快得到解决,但服务器在之后的每个请求中都显示错误。 在重新启动服务器之前,代码更改尚未反映出来。

touch tmp/restart.txt是不够的,所以我不得不重新启动服务器/etc/init.d/apache2 restart以删除错误消息。

 $ apache2 -v
服务器版本:Apache / 2.2.16(Debian)
服务器内置:2012年4月1日07:14:38

 $ ruby​​ -v
 ruby 1.9.3p194(2012-04-20修订版35410)[x86_64-linux]

 $ gem list passenger
 ***当地gem***
乘客(3.0.13)

 $ bundle exec rails -v
 Rails 3.0.7