2012-10-05 47 views
2

我試圖建立一個認證系統爲我的應用Rails 3中 - 錯誤的參數數目(0 1)

註冊用戶填寫一個表格,然後提交的呼叫用戶#註冊

def signup 
    @user = User.new(params[:user]) 
    @user.password(params[:user][:password]) 
    @user.save 
end` 

我的用戶模型包含

def password(pass) 
    @password=pass 
    self.salt = User.random_string(10) 
    self.hashed_password = User.encrypt(@password, self.salt) 
end 

當試圖調試,我檢查什麼是包含在我的@user哈希:

  • @user = User.new(params[:user])後哈希包含我的參數exept「身份證」,「hashed_pa​​ssword」和「鹽」(加時間戳),這是空

  • @user.password(params[:user][:password])後的散列現在包含「hashed_pa​​ssword」和「鹽值'

  • @ user.save返回

    引發ArgumentError在UsersController#註冊

    錯誤的參數數目(0 1)

堆棧跟蹤

Started POST "https://stackoverflow.com/users/signup" for 127.0.0.1 at Fri Oct 05 14:23:49 +1000 2012 
Processing by UsersController#signup as HTML 
    Parameters: {"user"=>{"last_name"=>"last", "first_name"=>"first", "login"=>"myusername", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"[email protected]", "dob(1i)"=>"1980", "dob(2i)"=>"4", "dob(3i)"=>"2"}, "authenticity_token"=>"R8/BNPox9F9rkUXZQ84xjnsplRjqLJYe35EtGjKEAWk=", "utf8"=>"✓", "commit"=>"Create User"} 
    (0.1ms) begin transaction 
    (0.0ms) rollback transaction 
Completed 500 Internal Server Error in 10ms 

ArgumentError (wrong number of arguments (0 for 1)): 
    app/controllers/users_controller.rb:17:in `signup' 
    app/controllers/users_controller.rb:16:in `signup' 


    Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms) 
    Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms) 
    Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.8ms) 

任何想法?

+0

您可以添加錯誤的堆棧跟蹤嗎? –

+0

我已將它添加到問題 – mreysset

+0

您的密碼方法沒有=。不應該是'def password =(pass)'? – HungryCoder

回答

1

我猜你有一個名爲password的列和/或你有一個驗證引用:password

在這種情況下,問題是您已經重寫password - 隱式零參數獲取器 - 與password(pass)(單參數設置器)。而應該將你的二傳手到password=,並作出一個getter,它總是返回零:

def password 
    nil 
end 

def password=(pass) 
    @password = pass 
    self.salt = User.random_string(10) 
    self.hashed_password = User.encrypt(@password, self.salt) 
end 

作爲獎勵,這意味着你可以消除明確的密碼設置呼叫,因爲User.new(:password => 'xyz')會自動調用password=

+0

謝謝,只是修改了setter結束getter函數返回了'錯誤的參數數量(0 for 1)'。但刪除對@ user.password的調用解決了它。 – mreysset

相關問題