2

我正在尝试为我的应用程序构建身份验证系统

注册用户填写表格,然后提交调用 user#signup

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 'id'、'hashed_pa​​ssword' 和 'salt'(加上时间戳)之后,它们是空的

  • 哈希后@user.password(params[:user][:password])现在包含“hashed_pa​​ssword”和“salt”的值

  • @user.save 返回

    用户控制器中的参数错误#signup

    参数数量错误(0 代表 1)

堆栈跟踪

Started POST "/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"=>"me@gmail.com", "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)

任何的想法?

4

1 回答 1

1

我猜你有一个名为的列password和/或你有一个验证引用:password

在这种情况下,问题在于您已经password用单参数设置器覆盖了隐式零password(pass)参数获取器。您应该将您的 setter 移至password=,并创建一个始终返回 nil 的 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=自动调用。

于 2012-10-05T04:58:48.397 回答