Possible Duplicate:
Can't convert String into integer in ruby/ruby-on-rails用戶身份驗證試圖字符串轉換爲整數
我正在通過教程林達學習軌和我遇到的一個問題。我已經構建了一個AccessController以與我擁有的AdminUser模型一起工作。我正在使用字符串/時間鹽組合並將其保存到Db,然後嘗試使用登錄表單進行身份驗證。下面是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
末
然後一些從我的模型,我定義的身份驗證方法:
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方法確實是以鹽從數據庫中進行該用戶並確保輸入的密碼被鹽分加密/加密。這裏是我不斷得到的錯誤:
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'
任何幫助將不勝感激。我確定我正在做與教程完全一樣的東西,但是由於Ruby/Rails版本的不同,可能會有錯誤?在這方面搜索了幾個小時,其中大部分討論inject()方法的結果都沒有幫助。再次感謝!
這個答案解決了我非常相同的情況。原因是'''User.hash''覆蓋'''Object.hash''' – masterweily 2013-03-07 09:23:07