Ruby on rails 未定义的方法“salt=';
我正在尝试向我的网站添加密码。但当我注册帐户时,按下注册按钮后显示的页面是一个错误页面,显示: 未定义的方法Ruby on rails 未定义的方法“salt=';,ruby-on-rails,ruby,web-applications,Ruby On Rails,Ruby,Web Applications,我正在尝试向我的网站添加密码。但当我注册帐户时,按下注册按钮后显示的页面是一个错误页面,显示: 未定义的方法salt='for# app/models/user.rb:34:inencrypt\u password' app/controllers/users\u controller.rb:19:in'create' 在my user.rb中,我有: def encrypt_password self.salt = make_salt if new_record? se
salt='for#
app/models/user.rb:34:in
encrypt\u password'
app/controllers/users\u controller.rb:19:in'create'
在my user.rb中,我有:
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
在users_controller.rb中,我有
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to PennTwit"
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end
错误与以下行有关:
if @user.save
self.salt = make_salt if new_record?
我试着将self.salt改为@salt,但它给了我另一个错误,在中说salt
安全散列(“#{salt}--#{string}”)是
没有找到
有什么想法吗?谢谢 您应该在您的模型中将
salt
属性设置为attr\u accessor
attr\u accessor:salt
这意味着salt
是一种未定义的方法
我认为您的数据库users
表中没有salt
字段
在gem文件add Bcrypt中显示用户表结构(或至少迁移),它负责加密/解密 gem“bcrypt ruby”,:require=>“bcrypt” 现在编辑您的方法
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
你在使用插件吗?(这是restful_认证吗?)。什么是restful_认证?(这是最近的老消息,但您的代码看起来非常相似,一直到salt/密码之间的双破折号)。查看您的供应商/插件目录。我尝试了这一点,但发现了一个新错误:未定义的方法“encrypted\u password=”,这很奇怪,因为salt在这个方法中,它以前没有检测到这个错误。对encrypted\u passwordattr\u accessor创建的自动获取,set和salt=methods.rails生成了迁移add_salt_to_users salt:string这应该会将它添加到数据库中,不是吗?但我仍然得到这个错误。