2012-01-28 47 views
0

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()方法的結果都沒有幫助。再次感謝!

回答

1

我有這個相同的問題,並花了數小時試圖解決它。原來,我的方法已經離開的教程,叫我刪除了一些視頻事先:

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

+0

這個答案解決了我非常相同的情況。原因是'''User.hash''覆蓋'''Object.hash''' – masterweily 2013-03-07 09:23:07

0

下面是一些常見的方式將造成錯誤:

my_method :name => "dan", :age => 23 

def my_method(*args) 
    puts args[:name] # ERROR. thinks that args looks like {:name => "dan3, :age => 23} 
    puts args[0][:name] # SUCCESS. args is actually an array because of the *splat: 
         # args = [{:name => "dan", :age => 23}] 
end 

或者

params = {} 
params[:greeting] = "howdy" 
params[:greeting][:bye] # ERROR. You're basically calling "howdy"[:bye]. 
         # Note that "howdy"[0] returns "h". 

這不是一個答案,但它會是難讀註釋。也許它可以幫助你看到尋找什麼。

例如,您傳遞字符串的某些代碼可能會期望散列。