我想验证用户是否存在,如果存在,请进行密码匹配。
我的控制器看起来像这样:
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'
如果它可以提供帮助,那么这就是development.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
变量来更好地保护用户的页面。这是存储在数据库中的东西。
答案 0 :(得分:0)
显然代码没有任何问题。它可能与我的浏览器缓存或某种性质有关,因为它在第二天就开始工作了。很抱歉给您带来不便。
答案 1 :(得分:0)
我遇到了同样的问题,花了好几个小时试图修复它。事实证明,我已经离开了一个方法,教程告诉我事先删除了一些视频:
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。
答案 2 :(得分:0)
只是想让你知道我也有这个问题。 问题很快得到解决,但服务器在之后的每个请求中都显示错误。 在重新启动服务器之前,代码更改尚未反映出来。
touch tmp/restart.txt
还不够,所以我不得不重新启动服务器/etc/init.d/apache2 restart
以删除错误消息。
$ apache2 -v Server version: Apache/2.2.16 (Debian) Server built: Apr 1 2012 07:14:38 $ ruby -v ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] $ gem list passenger *** LOCAL GEMS *** passenger (3.0.13) $ bundle exec rails -v Rails 3.0.7